dre*_*ale 15 android android-layout android-relativelayout
我有一个relativeLayout如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:id="@+id/parent" >
<ListView
android:layout_width="360dp"
android:layout_height="600dp"
android:id="@+id/list"
android:inputType="text"
android:maxLines="1"
android:layout_margin="50dp"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
在java代码中,我想在listview的左侧添加一个视图,但它没有用:
m_relativeLayout = (RelativeLayout)findViewById(R.id.parent);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.LEFT_OF, m_listView.getId());
Button button2 = new Button(this);
button2.setText("I am button 2");
m_relativeLayout.addView(button2, layoutParams);
Run Code Online (Sandbox Code Playgroud)
只有当我将listview设置为alignParentRight时,它才会起作用.这是一个机器人的bug还是我错过了什么?
我总是尝试addView(View child, int index, LayoutParams params)
,但它可能只适用于线性布局.那么有一个正常的解决方案来完成RelativeLayout.LEFT_OF
工作吗?
编辑
我曾尝试RelativeLayout.BELOW
和RelativeLayout.RIGHT_OF
,他们的工作完美,所以这意味着我没有足够的地方,以获得该按钮?我试图给予更多空间,但它仍然不起作用.
我使用东芝AT100(1280*800)和风景,所以空间足够.测试below
和和.right
一样left
.我想如果我把一个控制器A放在relativelayout中,那么我在控制器A的左边添加控件B和decalare,结果应该是控件B将控件A推到右边,对吗?
Luk*_*rog 23
我想如果我把一个控制A放在relativelayout中,那么我添加控件B并声明它在控件A的左边,结果应该是控件B将控件A推到右边,对吗?
您的假设不正确,除非您使用RelativeLayout.LayoutParams
规则指定,否则控件A不会被推到右侧.RelativeLayout
如果您没有为它们指定放置规则,则从屏幕的左上角开始将其子项放在彼此的顶部.当您将View
A 添加到RelativeLayout
没有任何规则(例如layout_alignParentRight
)时,它将从屏幕的左上角开始放置.然后,当您添加View
B时,规则to_leftOf
将应用于此View
位置,但此规则对于View
将保持其在屏幕上的位置的A 没有任何意义.这将使View
B位于View
A 的左侧但位于屏幕外部,因为View
A边界从屏幕的左边界开始.
在Button
将被放置到的左侧ListView
,当你使用layout_alignParentRight="true"
,因为现在有空间,实际看到的Button
(它不再外).addView(View child, int index, LayoutParams params)
在a中工作LinearLayout
因为LinearLayout
它将子项排成行或列(取决于方向)所以当你View
在特定位置添加一个时,它会将另一个推Views
到右边或下面(取决于方向)(没有亲戚)将观点定位在a中LinearLayout
,唯一的规则就是孩子们一个接一个地来.
从ListView
没有设置任何规则开始,这里有一个如何让它Button
出现在左边的示例ListView
:
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
Button button2 = new Button(this);
button2.setText("I am button 2");
button2.setId(1000);
m_relativeLayout.addView(button2, layoutParams);
RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) m_listView
.getLayoutParams();
rlp.addRule(RelativeLayout.RIGHT_OF, button2.getId());
Run Code Online (Sandbox Code Playgroud)
按钮将正常添加到屏幕上,它将从屏幕的左上角开始显示.没有上面代码中的两行Button
并且ListView
将重叠,因为这是RelativeLayout
没有任何规则的子项的正常行为.然后我们明确地修改它的位置ListView
以将其向右移动(使用上面代码中的最后两行).
归档时间: |
|
查看次数: |
43650 次 |
最近记录: |