繁体   English   中英

如何自定义布局(高度和宽度)和布局的容器?

[英]How to Customize the Layout (height and Width), and containership of layouts?

我创建了一个包含两行的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>

避免嵌套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共享的公共布局。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM