【问题标题】:Linearlayout overlay another linearlayout线性布局覆盖另一个线性布局
【发布时间】:2013-06-04 17:15:46
【问题描述】:

动态添加线性布局有一些问题。它添加在屏幕顶部,覆盖其他线性布局。

这里是 XML、代码和结果。

XML:

<TextView
    android:id="@+id/top_km"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:background="#888"
    android:gravity="top"
    android:textColor="#fff"
    android:textSize="30dip"
    android:layout_centerHorizontal="true"/>

<RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/top_km"
        android:id="@id/textLayout">

</RelativeLayout>

代码:

myLayout = (RelativeLayout) page.findViewById(R.id.textLayout);
LinearLayout linLayout = new LinearLayout(this);  
linLayout.setOrientation(LinearLayout.VERTICAL);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
myLayout.addView(linLayout);
LinearLayout hozLayout = new LinearLayout(this);
hozLayout.setOrientation(LinearLayout.HORIZONTAL);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
myLayout.addView(hozLayout);

结果: enter link description here

谢谢

【问题讨论】:

    标签: android android-layout layout overlay android-linearlayout


    【解决方案1】:

    不要使用RelativeLayout 作为您的持有人。请改用 LinearLayoutorientation="vertical"

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/top_km"
        android:orientation="vertical"
        android:id="@id/textLayout" />
    

    然后在代码中

    myLayout = (LinearLayout) page.findViewById(R.id.textLayout);

    紧随其后

    // rest of your code
    

    【讨论】:

      【解决方案2】:

      这是因为您使用 RealativeLayout 来正确添加使用 1.RelativeLayout.LayoutParams for st LayoutParams 2. 在 LayoutParams 中使用 below

      字段

      例子:

      RelativeLayout rl=new RelativeLayout(this);
      LinearLayout ll1=new LinearLayout(this);
      TextView tx1=new TextView(this);
      tx1.setText("Test1");
      ll1.addView(tx1);
      rl.addView(ll1);
      LinearLayout ll2=new LinearLayout(this);
      TextView tx2=new TextView(this);
      tx2.setText("Test1");
      ll2.addView(tx1);
      rl.addView(ll2);
      RelativeLayout.LayoutParams lp2=(LayoutParams) ll2.getLayoutParams();
      

      然后使用lp2.addRule

      这里有一些帮助:

      参数 verb 由 RelativeLayout 定义的动词之一,例如 ALIGN_WITH_PARENT_LEFT。 anchor 另一个视图的 id 用作锚点,或布尔值(表示为 TRUE)表示真或 0 表示假)。对于不引用另一个兄弟的动词(例如,ALIGN_WITH_PARENT_BOTTOM),只需使用 -1。

      【讨论】:

      • 我试过这样做:RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); params.addRule(RelativeLayout.ALIGN_BOTTOM,id_value_layout-1); hozLayout.setLayoutParams(params);但情况是一样的。
      【解决方案3】:

      也许您更容易使用android:visibility="GONE" 将其添加到 XML 文件中,然后在代码中显示它 (View.VISIBLE) 或隐藏它 (View.GONE)。

      【讨论】:

      • 否((我不知道LinearLayout的数量。可能是10也可能是100。
      • 那么,为什么不使用 ListView,将您的布局作为行,更容易编码。无论如何,如果您仍想以编程方式执行此操作(不鼓励这样做,请参阅此处 stackoverflow.com/questions/9827819/…),然后按照 Ken Wolf 所说的那样,使用 LinearLayout 作为容器
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多