Android 在相对布局的动态按钮之间绘制线

Android 在相对布局的动态按钮之间绘制线,android,android-layout,android-canvas,Android,Android Layout,Android Canvas,在动态按钮之间绘制线条 我的线性布局中有三个相对布局 Layout 1具有主按钮,当单击主按钮上执行的操作生成相对Layout 2到代码中的动态按钮时 我的问题是,我是否可以使用graphics类,通过在布局中以动态方式绘制线条,将主按钮与其子按钮连接起来?将id设置为动态按钮,并使用带有按钮id的addRule()将线条添加到按钮下方 <LinearLayout android:orientation="horizontal" android:layout_width=

在动态按钮之间绘制线条

我的线性布局中有三个相对布局

Layout 1
具有主按钮,当单击主按钮上执行的操作生成相对
Layout 2
到代码中的动态按钮时


我的问题是,我是否可以使用graphics类,通过在布局中以动态方式绘制线条,将主按钮与其子按钮连接起来?

将id设置为动态按钮,并使用带有按钮id的addRule()将线条添加到按钮下方

<LinearLayout 
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="6"
    android:gravity="center"
    android:paddingLeft="5dp">

    <RelativeLayout
        android:id="@+id/child_one"  
        android:layout_height="match_parent"
        android:layout_width="0dp"
        android:gravity="center"
        android:layout_weight="0.7">

        <Button android:id="@+id/buttonhome1"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_centerInParent="true"
             android:clickable="true"
             android:focusable="false"
             android:focusableInTouchMode="false"
             android:background="@drawable/"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:layout_below="@+id/buttonhome1"
            android:text="@string/Main"/> 

     </RelativeLayout>

    <RelativeLayout
        android:id="@+id/child_two"
        android:layout_height="match_parent"
        android:layout_width="0dp"
        android:gravity="center"
        android:visibility="invisible"
        android:layout_weight="1.8">

        <Button android:id="@+id/childBtn1"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:focusable="true" 
             android:layout_marginTop="10dp"
             android:layout_marginLeft="0dp"
             android:background="@drawable/"/>

        <TextView
            android:id="@+id/childtxt1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:gravity="center_horizontal"
            android:layout_centerVertical="@+id/childBtn1"
            android:layout_toRightOf="@+id/childBtn1"
            android:text="@string/text1"/>  

        <Button android:id="@+id/childBtn2"
            android:layout_width="wrap_content"
            android:layout_height=  "wrap_content"
            android:focusable="true" 
            android:layout_marginTop="10dp"
            android:layout_marginLeft="80dp"
            android:layout_below="@+id/childBtn1"
            android:background="@drawable/"/>

        <TextView
            android:id="@+id/childtxt2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:gravity="center_horizontal" 
            android:layout_below="@+id/childBtn1"
            android:layout_toRightOf="@+id/childBtn2"
            android:text="@string/text2"/>  

    </RelativeLayout>
    <RelativeLayout
        android:id="@+id/child_three"
        android:layout_height="match_parent"
        android:visibility="visible"
        android:layout_width="0dp"
        android:layout_weight="1.8" />
     <RelativeLayout
        android:id="@+id/child_four"
        android:layout_height="match_parent"
        android:layout_width="0dp"
        android:visibility="visible"
        android:layout_weight="1.7"/>        
</LinearLayout>

添加按钮时,您可以添加一个黑色(根据需要)背景的视图,并在相对布局中添加layout_width=“match_parent”,layout_height=“1dp”。我使用
TextView
background
颜色和
height=2dp
:DIf这样做,线条将仅在特定布局中用作元素。它如何从布局1连接到布局2。布局使用布局权重进行分隔。例如:我需要在布局中使用节点树之类的内容发布xml,以便更好地理解问题。谢谢sripathi…但我的要求不同。将线性布局水平拆分为三个相对布局。这些相对布局我有动态按钮。现在我需要以线的形式从相对布局1中的按钮连接到相对布局2中的其他按钮。您是否使用相对布局单独保持按钮?还有其他的看法吗?
    RelativeLayout rLayout = (RelativeLayout) findViewById(R.id.your_relative_layout);
    RelativeLayout.LayoutParams lparams = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
    // your button here
    Button tv1 = new Button(this);
    tv1.setText("Hello");
    tv1.setLayoutParams(lparams);
    tv1.setId(1);//id of the button
    rLayout.addView(tv1);
   // line here
   RelativeLayout.LayoutParams lineparams = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.MATCH_PARENT, 1);
   View line = new View(this);
   lineparams.addRule(RelativeLayout.BELOW, 1);//specify the id of the button to add the line below the button
   line.setLayoutParams(lineparams);
   line.setBackgroundColor(getResources().getColor(android.R.color.black));
   line.setId(2);
   rLayout.addView(line);