我创建了一个包含两行的 TableLayout
,其中第一行有一个 TextView
,第二行有两列,第一行有一个 ListView
第二个在不同的行中有一个 EditText
和一个 Button
。我希望两行的高度相等(正好是屏幕的一半),第二行的两列的宽度应该相等(正好是屏幕的一半)。我想构建这样的东西:
我应该如何进行?我应该选择哪种布局?
最佳答案
我认为您当前的解决方案行不通(因为需要相等的空间(宽度和高度)+ ListView
存在)。一种解决方案是使用嵌套的 weights
(这对性能不利,但(可能,不知道你在做什么)不是会破坏你的应用程序的重要东西)如下面的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="@layout/identical_layout" />
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="TextView" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<ListView
android:id="@+id/list"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" >
</ListView>
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ddd" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
避免嵌套权重
问题的另一种方法是使用如下所示的RelativeLayout
(我还没有测试过):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<include
android:id="@+id/included_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="@layout/identical_layout" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/included_layout" >
<View
android:id="@+id/anchor"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerVertical="true" />
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_above="@id/anchor"
android:layout_alignParentTop="true"
android:background="#99cc00"
android:text="TextView" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_alignParentBottom="true"
android:layout_below="@id/anchor" >
<ListView
android:id="@+id/list"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" >
</ListView>
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ddd" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
identical_layout
布局文件表示您的Activity
共享的通用布局。
关于android - 如何自定义布局(高度和宽度)以及布局的容器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10779802/