relativeLayout 添加规则“RelativeLayout.LEFT_OF”不起作用
-
13-12-2019 - |
题
我有一个如下所示的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>
在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);
仅当我将列表视图设置为 父右对齐, ,它会起作用。这是 Android 的错误还是我遗漏了什么?
我总是尝试 addView(View child, int index, LayoutParams params)
, ,但它可能只适用于线性布局。那么有没有一个正常的解决方案可以使 RelativeLayout.LEFT_OF
工作?
编辑
我努力了 RelativeLayout.BELOW
和 RelativeLayout.RIGHT_OF
, ,而且它们工作得很好,所以这意味着我没有足够的地方来获取按钮?我尝试给予更多空间,但仍然不起作用。
我用的是东芝AT100(1280*800)和横屏,所以空间足够了。测试 below
和 right
就像 left
. 。我想如果我将一个控件A放在相对布局中,然后我添加控件B并将其声明在控件A的左侧,结果应该是控件B会将控件A推到其右侧,对吧?
解决方案
我想如果我将一个控件A放在相对布局中,然后添加控件B并声明它位于控件A的左侧,结果应该是控件B会将控件A推到其右侧,对吧?
你的假设是不正确的,控件 A 不会被推到右边,除非你用 a 指定它 RelativeLayout.LayoutParams
规则。 RelativeLayout
如果您没有为子级指定放置规则,则从屏幕左上角开始将其子级放置在一个顶部。当您添加 View
一个到 RelativeLayout
没有任何规则(比如 layout_alignParentRight
)它将从屏幕的左上角开始放置。然后,当您添加 View
B、规则 to_leftOf
将适用于此 View
的立场,但这条规则对 View
一个谁将保持其在屏幕上的位置。这将使 View
B 位于 的左侧 View
A 但在屏幕之外 View
边界从屏幕的左边框开始。
这 Button
将被放置在左侧 ListView
当你使用 layout_alignParentRight="true"
因为现在有空间可以实际看到 Button
(不再是外面了)。 addView(View child, int index, LayoutParams params)
工作于 LinearLayout
因为 LinearLayout
将其子项排列在行或列中(取决于方向),因此当您添加 View
在特定的位置,它会推动另一个 Views
在其右侧或下方(取决于方向)(视图中没有相对定位 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());
该按钮将像平常一样添加到屏幕上,并且将从屏幕的左上角开始显示。如果没有上面代码中的两行 Button
和 ListView
将重叠,因为这是正常行为 RelativeLayout
对于没有任何规则的孩子。然后我们显式修改 ListView
将其移至右侧(使用上面代码的最后两行)。
其他提示
如果您的变量名称是指示性的,那是因为您要将小部件添加到 LinearLayout,因此relativelayout 的标签将被忽略。
我正在谈论的就是这一行:
m_linearLayout.addView(button2, layoutParams);
编辑
你说 alignParentRight
作品...唯一的区别是 ot 不采用锚参数。也许 m_listView.getId()
没有返回正确的 id。您可以使用调试器逐步执行并查看它是否返回正确的值。
也许你可以尝试专门调用 id...
layoutParams.addRule(RelativeLayout.LEFT_OF, R.id.list);
要执行此操作,请使用预定义视图 ID 或声明一个。在 值文件夹 创造 id.xml 然后添加一个 物品 像这样:
<item name="imageViewID" type="id"/>
在您创建新视图实例的代码中使用此 id,如下所示:
RelativeLayout layout=new RelativeLayout(context);
ImageView imageView = new ImageView(context);
imageView.setId(R.id.imageViewID);
RelativeLayout.LayoutParams layoutParams = new LayoutParams(50, 50);
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
layout.addView(imageView, layoutParams);
TextView textView = new TextView(context);
RelativeLayout.LayoutParams textViewParams= new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
textViewParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
textViewParams.addRule(RelativeLayout.BELOW, imageView.getId());
layout.addView(nameView, nameLayoutParams);
或者我们可以直接使用这个函数 View.generateViewId() 执行相同的操作。像这样:
imageView.setId(View.generateViewId());
我想你可能忘记添加 m_listView
到 RelativeLayout
或者 m_listView
的可见度是 GONE
.
你能检查一下吗?
setId 在align 之前被调用,特别是对于新的对象视图。
如果您使用的是自定义 ID,而不是常规生成的 Android ID(例如 R.id.my_id
),确保id不等于0(或负数),否则该规则将被忽略。