【问题标题】:How to Customize the Layout (height and Width), and containership of layouts?如何自定义布局(高度和宽度)以及布局的容器?
【发布时间】:2012-06-02 12:49:58
【问题描述】:

我创建了一个有两行的TableLayout,其中第一行有一个TextView,第二行有两列,第一行有一个ListView,第二个有一个EditText和一个Button在不同的行中。我希望两行高度相等(正好是屏幕的一半),并且第二行的两列应该是相等的宽度(正好是屏幕的一半)。我想构建这样的东西:

我应该如何进行?我应该选择哪种布局?

【问题讨论】:

    标签: android android-layout android-widget


    【解决方案1】:

    我认为您当前的解决方案行不通(因为需要相等的空间(宽度和高度)+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>
    

    避免嵌套weights 问题的另一个选择是使用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 布局文件代表您的Activities 共享的通用布局。

    【讨论】:

    • +1000 谢谢你真的很棒,目前我正在尝试使用 TableLayout 来实现这一点,但是你的回答让我很容易,我正在尝试实现第一个选项......如果他们是一些性能开销我肯定会选择第二种方法...再次感谢...
    • 那工作...现在唯一的问题是 ListView 不滚动,我该怎么做???再次感谢十亿...
    • @anDroider 我不知道为什么你的ListView 不滚动。有一些与您的代码相关的东西,而不是上面的布局文件,因为我现在测试它们并且ListView 按预期工作..
    • 它是第一次工作,但如果一旦焦点到达 EditText,ListView 将不会滚动...
    • 我使用与您的回答中提到的相同的代码..(嵌套权重)出于说明目的我设置了一些示例字符串值(如我同事的随机名称),一切正常(所有谢谢你)......但是一旦焦点进入最后一个LinearLayout,之后即使我多次单击ListView也不会滚动......我正在模拟器上测试它的API 2.3.3(10) ...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-11
    • 2013-02-16
    • 1970-01-01
    相关资源
    最近更新 更多