[英]Android - LinearLayout/RelativeLayout Alignment
I am attempting to create a simple UI that looks something like this 我正在尝试创建一个看起来像这样的简单UI
I am very new to android and have been trying different types of layouts (Linear/Relative) and I believe Linear may be the way to go here (I hope to make the amount of ListViews dynamic at some stage but for now 2 is fine). 我是android的新手,一直在尝试不同类型的布局(线性/相对),我相信线性可能是解决问题的方法(我希望在某个阶段使ListViews的数量动态化,但现在2很好) 。
Should I be using a Linear Layout or Relative layout, or some combination of both to achieve this. 我应该使用线性布局还是相对布局,或两者结合使用以实现此目的。
Here is the XML which I have, I can't seem to get the button to align right, even though its gravity is set to right. 这是我所拥有的XML,即使其重力设置为正确,我似乎也无法使该按钮正确对齐。 I am open to any suggestions on how to fix this, or if there is a better way 我愿意就如何解决此问题或是否有更好的方法提出任何建议
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.9"
android:orientation="horizontal" >
<ListView
android:id="@+id/lv1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
/>
<ListView
android:id="@+id/lv2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="New Button"
android:id="@+id/button"
android:gravity="right" />
</LinearLayout>
edit : here is what the UI currently looks like 编辑:这是用户界面当前的外观
OK, you can do one thing. 好,你可以做一件事。
<RelativeLayout>
<LinearLayout> <!-- horizontal orientation with weightsum 2 -->
<LinearLayout> <!-- vertical orientation with layout weight 1 and width 0dp -->
</LinearLayout>
<LinearLayout> <!-- vertical orientation with layout weight 1 and width 0dp -->
</LinearLayout>
</LinearLayout>
<Button /> <!-- alignParentBottom = true and alignParentLeft = true -->
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/ll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_marginTop="18dp"
android:layout_above="@+id/btnButton"
android:weightSum="2"
android:orientation="horizontal" >
<ListView
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1">
</ListView>
<ListView
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1">
</ListView>
</LinearLayout>
<Button
android:id="@+id/btnButton"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Button"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
Just remove the LinearLayout around the Button or set its orientation to vertical. 只需删除Button周围的LinearLayout或将其方向设置为垂直即可。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.9"
android:orientation="horizontal" >
<ListView
android:id="@+id/lv1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"/>
<ListView
android:id="@+id/lv2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="New Button"
android:id="@+id/button"
android:gravity="right" />
</LinearLayout>
Avoid layout nesting . 避免布局嵌套 。
A single RelativeLayout is enough. 一个单一的RelativeLayout就足够了。
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!-- Set the Button, first: Bottom and right aligned -->
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="New Button"
/>
<!-- Trick: set a dummy View in the middle of the screen -->
<View
android:id="@+id/dummy"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerInParent="true"
/>
<!-- Now the ListViews: -->
<!-- One above the button and to the left of the dummy -->
<ListView
android:id="@+id/lv1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/button"
android:layout_toLeftOf="@id/dummy"
/>
<!-- One above the button and to the right of the dummy -->
<ListView
android:id="@+id/lv2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/button"
android:layout_toRightOf="@id/dummy"
/>
</RelativeLayout>
我认为您可以尝试这种方式....
<LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_gravity="right" android:layout_weight="0.1" android:gravity="right" android:orientation="horizontal" > <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button" /> </LinearLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.