看似相似的RelativeLayouts代码片段的工作方式不同

时间:2015-07-29 19:25:47

标签: android relativelayout

两件作品都是将两个物体彼此靠近放置,其中一个物体的尺寸固定

第一件。按预期工作:动态填充scrollView占用键盘上方的所有空间

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
root.addView(keyboardView, params);
params = new RelativeLayout.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
params.addRule(RelativeLayout.ABOVE, keyboardView.getId());
root.addView(scrollView, params);

第二部分。假设在右侧放置一个按钮,并将editText放在按钮左侧的其余部分。相反,editText忽略按钮并占据整个屏幕宽度,因此按钮位于其下

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.MATCH_PARENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
container.addView(deleteButton, params);
params = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
params.addRule(RelativeLayout.LEFT_OF, deleteButton.getId());
container.addView(editText, params);

第二部分用XML重新制作。出于某种原因,根据需要工作

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="Delete"
        android:id="@+id/item_deleteButton"
        android:layout_alignParentRight="true"
        android:layout_alignBottom="@+id/item_editText" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/item_editText"
        android:layout_toLeftOf="@+id/item_deleteButton" />
</RelativeLayout>

0 个答案:

没有答案
相关问题