簡體   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