开发者

LinearLayout problem

开发者 https://www.devze.com 2023-02-18 10:35 出处:网络
I\'m trying to create a layout containing, among other things, a LinearLayout. The XML for the whole screen is:

I'm trying to create a layout containing, among other things, a LinearLayout. The XML for the whole screen is:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout android:id="@+id/fileSelView"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="fill_parent">

    <Spinner android:id="@+id/dirListSpinner"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"/>

    <Spinner android:id="@+id/fileTypeSpinner"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"/>

    <EditText android:id="@+id/fileNameTF"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_toRightOf="@+id/fileTypeSpinner"/>

    <LinearLayout android:id="@+id/centerBox"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_below="@+id/dirListSpinner"
            android:layout_above="@+id/fileTypeSpinner"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true">

        <ListView android:id="@+id/dirView" android:background="#f00"
                android:layout_width="fill_parent" android:layout_height="fill_parent"
                android:layout_weight="1"/>

        <RelativeLayout android:id="@+id/buttonBox" android:background="#0f0"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:layout_weight="0">

            <Button android:id="@+id/upButton"
                    android:text="Up"
                    android:layout_width="wrap_content" android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true"/>

            <Button android:id="@+id/mkdirButton"
                    android:text="MkDir"
                    android:layout_width="wrap_content" android:layout_height="wrap_content"
                    android:layout_below="@+id/upButton"
                    android:layout_centerHorizontal="true"/>

            <Button android:id="@+id/okButton"
                    android:text="OK"
                    android:layout_width="wrap_content" android:layout_height="wrap_content"
                    android:layout_below="@+id/mkdirButton"
                    android:layout_centerHorizontal="true"/>

            <Button android:id="@+id/cancelButton"
                    android:text="Cancel"
                    android:layout_width="wrap_content" android:layout_height="wrap_content"
                    android:layout_below="@+id/okButton"
                    android:layout_centerHorizontal="true"/>

        </RelativeLayout>

    </LinearLayout>

</RelativeLayout>

The result of this layout looks like this:

LinearLayout problem

The LinearLayout itself is laid out the way I want, within its parent, but its contents come out all wrong. It has two children: a ListView on the left and a RelativeLayout on the right. The ListView should take up all the available height, and as much width as possible, while the RelativeLayout should be a small as possible and vertically centered within its parent. Instead, the ListView ends up being way too narrow, and the RelativeLayout grows to fill the space, despite the ListView having android:layout_weight="1" and the RelativeLayout having android:layout_weight="0". Also, the RelativeLayout is aligned with the top of its parent, despite having android:gravity="center_vertical". What am I doing wrong?


UPDATE

OK, I changed android:gravity="center_vertical" to android:layout_gravity="center" on the RelativeLayout, and now it is vertically centered within its parent, as desired.

Regarding the layout weight issue, I tried changing android:layout_width="fill_parent" to android:layout_width="0px" on the ListView, but that didn't work; I'm getting the same result as before, with the ListView way too narrow and the RelativeLayout expanding to take up the available space.

The layout now looks like this: http://thomasokken.com/layout-problem2.png

Note that the buttons in the RelativeLayout are not correctly centered horizontally. It's as if the RelativeLayout got sized and laid out correctly at first, and then grew towards the left later, without re-laying out its children.

I haven't been able to get the ListView to get sized properly using a RelativeLayout parent, either. Could it be resizing itself in response to a setAdapter() call? I'm using a custom ListAdapter class whose getView() method returns RelativeLayout objects:

public View getView(int position, View convertView, ViewGroup parent) {
    File item = items[position];
    if (convertView == null) {
        Context context = parent.getContext();
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.file_selection_dialog_row, null);
        ImageView icon = (ImageView) convertView.findViewById(R.id.fdrowimage);
        icon.setImageResource(item.isDirectory() ? R.drawable.folder : R.drawable.document);
    }
    TextView text = (TextView) convertView.findViewById(R.id.fdrowtext);
    text.setText(item.getName());
    return convertView;
}

The layout for the list rows looks like this:

<?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">

    <ImageView android:id="@+id/fdrowimage"
            android:layout_height="35dp" android:layout_width="wrap_content"
            android:layout_alignParentLeft="true"
            android:paddingRight="5dp" android:paddingLeft="3dp"/>

    <TextView android:id="@+id/fdrowtext"
            android:layout_width="wrap_content" android:layout_height="35dp"
            android:layout_toRightOf="@+id/fdrowimage"
            android:layout_alignTop="@+id/fdrowimage"
            android:layout_alignBottom="@+id/fdrowimage"
            android:gravity="center_vertical"
            android:textSize开发者_StackOverflow社区="23dp"/>

</RelativeLayout>


There are a few things going on here.

First as to the vertical centering of the RelativeLayout. android:gravity="center_vertical" indicates that the children of this view should have center_vertical applied. And it is actually working. As you can see by the size of the green background, your RelativeLayout is only as big as it needs to be to fit the buttons. You have two solutions. If you want the height of the view to stay the same and be centered inside its parent, you would use android:layout_gravity="center". If you want the RelativeLayout to fill the column then you need to set the layout_height of the RelativeLayout to be "fill_parent". android:layout_gravity applies to the view itself inside its parent. android:gravity applies to the view's children.

Second is the layout weight issue. The LinearLayout will first layout any wrap_content items (ie, your RelativeLayout), then it will apply children that have a layout_weight AND a size of 0. If you want your layout_weight to work properly, you need to set the layout_width of the ListView to "0px".

0

精彩评论

暂无评论...
验证码 换一张
取 消