如何自定义布局的布局(高度和宽度),以及集装箱?布局、自定义、集装箱、宽度

2023-09-04 05:16:45 作者:怀抱意中人

我创建了一个 TableLayout 两行,其中第一行有一个的TextView 和第二行有两列,第一个有一个的ListView ,第二个有一个的EditText 按钮每个不同行。欲两行相等的高度(屏幕正好一半)和两个第二行的列应是相等的宽度(屏幕正好一半)的。我想建立这样的事情:

I have created a TableLayout with two rows where first row has one TextView and second row has two columns, first one has a ListView and the second one has one EditText and a Button each in different rows. I want both rows of equal height (exactly half of the screen) and both of the columns of second row should be of equal width (exactly half of the screen). I Want to build something like this:

我应该如何着手?哪个版面,我应该选择?

How should I proceed ? Which layout I should choose?

推荐答案

我不认为你目前的解决方案,将工作,因为同等空间的要求(宽度和高度)+的的ListView( presence)。一种解决方案是使用嵌套的权重(这是不好的表现,但(可能是,不知道你做了什么),将打破你的应用程序不是那么重要),如在以下布局:

I don't think your current solution will work(because of the requirement for equal space(width and height) + the ListView presence). One solution is to use nested weights(which are bad for performance, but(probably, without knowing what you do) not something that important that will break your app) like in the layout below:

<?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的象下面这样(我没有测试它):

Another option to avoid the nested weights problem is to use a RelativeLayout like below(I haven't tested it):

<?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 布局文件重新presents公共布局由你的分享活动

The identical_layout layout file represents the common layout shared by your Activities.