簡體   English   中英

垂直線性布局中的重量

[英]Weight in Vertical Linear Layout

我對垂直線性布局中的重量感到困惑。 我可以在水平線性布局中很好地使用重量,但如何在垂直線性布局中使用它。 我有一個相對布局,其中包含一個襯里布局(垂直),有一些文本視圖和文本字段。我想要以下安排。

紅線=相對布局,綠線=線性布局(垂直),藍色=文本視圖,黃色=特殊字段

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center" >
    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/editText2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="numberDecimal" />
    </LinearLayout>
</RelativeLayout>

如果使用layout_weigth參數,則應該在match_parent上具有與線性布局方向相關的參數

所以:

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"

-首先,您可以在使用LinearLayout 情況下 直接Relative layout 進行排列。

-其次,如果你想在Relative layout有一個子布局,那么最好在你的Relative layout使用TableRow ,並在其中排列元素,就像你已經顯示的那樣。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center" >
     <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" 
        android:layout_margin="10dp">
    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/editText1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="numberDecimal" />

    </LinearLayout>
    <LinearLayout

        android:id="@+id/linearLayout2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/editText2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="numberDecimal" />

    </LinearLayout>
     <LinearLayout

        android:id="@+id/linearLayout3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/textView3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/editText3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="numberDecimal" />


    </LinearLayout>
     <LinearLayout

        android:id="@+id/linearLayout4"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/textView4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/editText4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="numberDecimal" />


    </LinearLayout>
    </LinearLayout>
</RelativeLayout>

我建議使用TableLayout 如果我有這個權利,那么您正在嘗試水平應用加權,以便在LinearLayout有常規列。 假設是對的,那么你永遠不會這樣做。

LinearLayout在其自身內水平或垂直排列所有元素。 它無法雙管齊下。 所以在任何情況下你都需要一個單獨的行容器來實現你的目標。

TableLayout不僅使用該內在行容器進行設計,而且還為這種布局提供內置列支持,而無需管理權重。

在我的情況下,在垂直權重問題的小游戲之后,我明白android將包裝ViewGroup的內容,即使你設置的權重比其內容的大小更大,它將包圍內容並給出其他視圖的額外空間。

解決方案是根據您的需求添加上邊距或下邊距。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM