我想在屏幕上创建一个包含2个元素的活动,一个在底部,有一个固定的大小,一个在其上面,一个必须填满屏幕。
如何在布局中执行此操作?
谢谢
答案 0 :(得分:3)
对第一个元素使用layout_weight="1"
并放置layout_height="0dp"
并使用linearlayout作为包含2个元素的父元素。
答案 1 :(得分:1)
或者你可以这样做,
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_alignParentTop="true"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_above="@+id/ll1"
>
</LinearLayout>
<LinearLayout
android:id="@+id/ll1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
>
</LinearLayout>
答案 2 :(得分:1)
使用以下代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="@string/hello_world" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="test" />
</LinearLayout>
答案 3 :(得分:1)
将其添加到第一个元素:
android:layout_weight="0.8"
和第二个
android:layout_weight="0.2"
答案 4 :(得分:1)
您可以使用相对布局,例如......以下示例
<//you are second element
android:id="@+id/ui_radiogroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:clickable="false"
android:orientation="horizontal" >
<///you are second element>
<//you are First element
android:id="@+id/ui_pager"
android:layout_width="wrap_content"
android:layout_above="@id/ui_radiogroup"
android:layout_height="wrap_content"
android:scrollbars="horizontal" />
其他选项使用线性布局根据您的要求为每个元素分配android:layout_weight
答案 5 :(得分:1)
试试这个,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/footertext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center"
android:text="Footer" />
<TextView
android:id="@+id/centertext"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/footertext"
android:gravity="center"
android:text="center" />
答案 6 :(得分:1)
这是完整的技巧:
RelativeLayout
包装您的两件物品layout_alignParentBottom="true"
放在底部项目layout_above="@+id/your_bottom_item_id"
在您的热门商品上以下是一个完整的工作示例,您可以cpy / paste:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/top_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_above="@+id/bottom_layout">
</LinearLayout>
<LinearLayout
android:id="@+id/bottom_item"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="vertical"
android:layout_alignParentBottom="true">
</LinearLayout>
</RelativeLayout>
答案 7 :(得分:1)
只需对第一个元素使用layout_weight =“1”,布局高度应为match_parent。