2017-02-14 3 views
0

У меня есть два вида, как показано на рисунке, первый я нарисовал круг и линии, второй у меня много кнопок, я хочу наложить два вида, чтобы каждая строка указывала на кнопкиОтносительная компоновка и пользовательский вид

enter image description here "Canvas"

[enter image description here]2

public class MainActivity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(new MyView(this)); 
} 
public class MyView extends View { 
    public MyView (Context context) { 
     super (context) ; 

    } 
@Override 
    protected void onDraw (Canvas canvas){ 
super.onDraw(canvas); 
int x = getWidth(); 
int y = getHeight(); 
int radius ; 
radius = 50 ; 
Paint paint = new Paint() ; 
Paint paint1 = new Paint() ; 
paint.setStyle(Paint.Style.FILL); 
paint.setColor(Color.WHITE); 
canvas.drawPaint(paint); 
paint.setColor(Color.BLACK); 
canvas.drawCircle(x/2, y/2, radius, paint); 

int margin = 500; 
int margin1 = 300; 
int top = 0 + margin; 
int bottom = canvas.getHeight() - margin; 
int left = 0 + margin1; 
int right = canvas.getWidth() - margin1; 
int centerX = x/2; 
int centerY = y/2; 

canvas.drawLine(centerX, top, centerX, bottom,paint1); 


canvas.drawLine(left, centerY, right, centerY,paint1); 

}}} 

RelativeLayout XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 


<Button 
    android:text="Button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:id="@+id/button1" /> 

<Button 
    android:text="Button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerVertical="true" 
    android:layout_alignParentStart="true" 
    android:id="@+id/button2" /> 

<Button 
    android:text="Button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignTop="@+id/button2" 
    android:layout_alignParentEnd="true" 
    android:id="@+id/button3" /> 

<Button 
    android:text="Button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignStart="@+id/button1" 
    android:id="@+id/button4" /> 

<Button 
    android:text="Button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/button1" 
    android:layout_alignParentStart="true" 
    android:layout_marginTop="49dp" 
    android:id="@+id/button5" /> 

<Button 
    android:text="Button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignTop="@+id/button5" 
    android:layout_alignStart="@+id/button3" 
    android:id="@+id/button6" /> 

<Button 
    android:text="Button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/button4" 
    android:layout_alignParentStart="true" 
    android:layout_marginBottom="58dp" 
    android:id="@+id/button7" /> 

<Button 
    android:text="Button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBottom="@+id/button7" 
    android:layout_alignParentEnd="true" 
    android:id="@+id/button8" /> 


</RelativeLayout> 
+0

Эти два изображения одинаковые. – Rachit

+0

@Rachit, Извините, я изменил – CamlX

+0

Есть 4 линии, но 8 кнопок? – Rachit

ответ

0

Не будет работать? Он добавит 4 кнопки в центрах по горизонтали и вертикали на концах линий, которые вы нарисовали.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

<Button 
    android:text="Button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:id="@+id/button1" /> 

<Button 
    android:text="Button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerVertical="true" 
    android:layout_alignParentStart="true" 
    android:id="@+id/button2" /> 

<Button 
    android:text="Button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerVertical="true" 
    android:layout_alignParentEnd="true" 
    android:id="@+id/button3" /> 

<Button 
    android:text="Button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:id="@+id/button4" /> 

</RelativeLayout> 
+0

Я использую пользовательский вид в основной деятельности, setContentView (новый MyView (this)); } public class MyView extends View {public MyView (контекст контекста) {супер (контекст); – CamlX

Смежные вопросы