【问题标题】:Drawing line between dynamic buttons across relative layout在相对布局的动态按钮之间画线
【发布时间】:2014-08-03 13:29:36
【问题描述】:
<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 1 有主按钮,当在主按钮上执行点击操作时,通过代码生成相对layout 2 中的动态按钮。

我的问题是,我可以使用图形类通过动态方式跨布局绘制线条来连接主按钮及其子按钮layout 2 吗?

【问题讨论】:

  • 您可以在添加按钮时在相对布局中添加具有黑色(根据需要)背景和 layout_width = "match_parent", layout_height="1dp" 的视图。
  • 我用TextViewbackground颜色和height = 2dp画线:D
  • 如果我这样做,行将仅在特定布局中作为元素。它如何从布局 1 连接到布局 2。布局使用 layout_weight 分隔。例如:我需要类似节点树的东西我的布局
  • 发布我们的 xml 以便更好地理解问题。

标签: android android-layout android-canvas


【解决方案1】:

为您的动态按钮设置 id 并使用带有按钮 id 的 addRule() 在您的按钮下方添加行。

    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);

【讨论】:

  • 谢谢 sripathi...但我的要求不同。用三个相对布局水平拆分线性布局。这些相对布局我有动态按钮。现在我需要从相对布局 1 中的按钮连接为线相对布局中的其他按钮 2.
  • 您是否使用相对布局单独按住按钮?你还有其他看法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-28
  • 1970-01-01
  • 1970-01-01
  • 2021-08-19
  • 2014-10-01
相关资源
最近更新 更多