繁体   English   中英

简单的Android布局问题

[英]Simple Android layout issue

我想做一些非常简单的事情。 我想要一个在顶部具有微调器的布局,其后是一个“列表视图”,在最底部具有一个线性布局,其中包含一些按钮。 无论窗口有多大,我都希望扩展列表视图以填充微调器和按钮之间的空间。 我一直在尝试使用包含所有三个元素的线性布局进行尝试,并且尝试了可以​​想到的Layout_Height的Wrap Content和Fill Parent的每种组合,但是除非我对列表视图Layout_Height进行硬编码说300 dip,否则将按下按钮离开屏幕。 我知道一定有一种简单的方法可以做到这一点,但是我不知所措。 我已经尝试了所有我能想到的。

这是适用于硬编码高度的代码。

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

<Spinner
    android:id="@+id/fileType"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

<ListView
    android:id="@+id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="300dip" />

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="bottom"
    android:gravity="bottom"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/ManageFiles_DeleteItem"
            android:layout_width="fill_parent"
            android:layout_height="40dip"
            android:layout_margin="5dip"
            android:layout_weight="1"
            android:text="Delete item" />

        <Button
            android:id="@+id/ManageFiles_DeleteAll"
            android:layout_width="fill_parent"
            android:layout_height="40dip"
            android:layout_margin="5dip"
            android:layout_weight="1"
            android:text="Delete all" />

        <Button
            android:id="@+id/ManageFiles_DisplayItem"
            android:layout_width="fill_parent"
            android:layout_height="40dip"
            android:layout_margin="5dip"
            android:layout_weight="1"
            android:text="Display item" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/ManageFiles_OKcustom"
            android:layout_width="fill_parent"
            android:layout_height="40dip"
            android:layout_margin="10dip"
            android:layout_weight="1"
            android:text="OK" />

        <Button
            android:id="@+id/ManageFiles_CancelCustom"
            android:layout_width="fill_parent"
            android:layout_height="40dip"
            android:layout_margin="10dip"
            android:layout_weight="1"
            android:text="Cancel" />
    </LinearLayout>
</LinearLayout>

`

您可以尝试一些简单的事情

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

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

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@id/spinner1"
    android:layout_above="@+id/button1" >
</ListView>

<Button
    android:id="@id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:text="Button1" />

<Button
    android:id="@id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:text="Button2" />

诀窍是使用RelativeLayout而不是LinearLayout。

使用以下

android:weightSum="Your total length" //in your main layout

android:layout_weight="" //in each of your listview,spinner,linearlayout

例如:如果您需要相等的空间来使用所有3个元素

android:weightSum="3"

然后在

Spinner
android:layout_weight="1"
/>

ListView
android:layout_weight="1"
/>

LinearLayout
android:layout_weight="1"
/>

使用权重,将2的权重赋给列表视图,将1的权重赋给微调器,并在底部的布局中包含按钮,然后您可以更改权重并查看哪种更适合您。

尝试这种方式,使其适合所有屏幕尺寸。

<?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"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/ftr_btn"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:gravity="bottom"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/ManageFiles_DeleteItem"
            android:layout_width="fill_parent"
            android:layout_height="40dip"
            android:layout_margin="5dip"
            android:layout_weight="1"
            android:text="Delete item" />

        <Button
            android:id="@+id/ManageFiles_DeleteAll"
            android:layout_width="fill_parent"
            android:layout_height="40dip"
            android:layout_margin="5dip"
            android:layout_weight="1"
            android:text="Delete all" />

        <Button
            android:id="@+id/ManageFiles_DisplayItem"
            android:layout_width="fill_parent"
            android:layout_height="40dip"
            android:layout_margin="5dip"
            android:layout_weight="1"
            android:text="Display item" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/ManageFiles_OKcustom"
            android:layout_width="fill_parent"
            android:layout_height="40dip"
            android:layout_margin="10dip"
            android:layout_weight="1"
            android:text="OK" />

        <Button
            android:id="@+id/ManageFiles_CancelCustom"
            android:layout_width="fill_parent"
            android:layout_height="40dip"
            android:layout_margin="10dip"
            android:layout_weight="1"
            android:text="Cancel" />
    </LinearLayout>
</LinearLayout>

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

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/ftr_btn"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/fileType" >
</ListView>

暂无
暂无

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

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