[英]Linearlayout overlay another linearlayout
我有一些动态添加linearlayout的问题。 它添加在屏幕顶部,覆盖其他线性布局。
这里是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);
结果: 在此输入链接描述
谢谢
这是因为你使用RealativeLayout进行正确的添加使用1.对于st LayoutParams的RelativeLayout.LayoutParams 2.在LayoutParams中使用下面的字段
例:
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)表示true,0表示false表示。 对于不引用另一个兄弟的动词(例如,ALIGN_WITH_PARENT_BOTTOM),只需使用-1。
不要使用RelativeLayout
作为持有者。 使用LinearLayout
与orientation="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
也许你更容易用android:visibility="GONE"
将它添加到XML文件中,然后在代码中显示它( View.VISIBLE
)或隐藏它( View.GONE
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.