2011-06-01 105 views
37

我知道無法設置百分比,您可以設置某些圖片的權重以調整其高度。我試圖做的是指定一個佈局相對於它所在佈局的高度。基本上我有類似的東西Android百分比佈局高度

<LinearLayout android:layout_height="fill_parent"> 
    <LinearLayout> 

    </LinearLayout> 
</LinearLayout> 

當然,這是一個非常簡化的版本,只是這樣你才能理解我的胡言亂語。基本上我想將內部線性佈局設置爲主線性佈局的50%左右。

這樣做的最好方法是什麼?

+0

使用支持庫,您現在可以使用PercentRelativeLayout和PercentFrameLayout:https://juliengenoud.github.io/android-percent-support-lib-sample/ – 2015-08-03 16:46:40

回答

15

您可以在其下添加另一個空白布局,並將它們設置爲具有相同的佈局權重。他們應該獲得每個空間的50%。

+1

哈,打我一分鐘。 :) – kcoppock 2011-06-01 15:29:38

8

正如你所說,我會推薦重量。百分比將是非常有用的(不知道爲什麼他們不支持),但你可以做到這一點的一種方式是像這樣:

<LinearLayout 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent" 
    > 
    <LinearLayout 
     android:layout_height="0dp" 
     android:layout_width="fill_parent" 
     android:layout_weight="1" 
     > 
    </LinearLayout> 
    <View 
     android:layout_height="0dp" 
     android:layout_width="fill_parent" 
     android:layout_weight="1" 
    /> 
</LinearLayout> 

外賣是,你有一個空的觀點,將佔用剩餘空間。不理想,但它確實符合你的要求。

89

有一個屬性叫android:weightSum。

您可以在內部linear_layout的父線性_layout和android:weight =「1」中設置android:weightSum =「2」。

請記住將內部linear_layout設置爲fill_parent,這樣weight屬性可以按預期工作。

順便說一句,我不認爲它需要添加第二個視圖,儘管我還沒有嘗試過。 :)

<LinearLayout 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent" 
    android:weightSum="2"> 

    <LinearLayout 
     android:layout_height="fill_parent" 
     android:layout_width="fill_parent" 
     android:layout_weight="1"> 
    </LinearLayout> 

</LinearLayout> 
+1

我忘了weightSum,好主意。 – kcoppock 2011-06-01 15:36:53

+1

我用它在類似的情況下,它的工作。 :) – 2011-06-01 15:41:57

+3

對我無效 – shoren 2012-09-15 05:07:35

7

機器人: 「YOURVALUE」 layout_weight =是個

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

    <TextView 
     android:id="@+id/logTextBox" 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight=".20" 
     android:maxLines="500" 
     android:scrollbars="vertical" 
     android:singleLine="false" 
     android:text="@string/logText" > 
    </TextView> 

</LinearLayout> 
+0

完美的作品,我希望它能自動在不同的尺寸上縮放。 – 89n3ur0n 2016-04-04 10:49:25

+0

@ 89n3ur0n是的,它是設備尺寸的百分比。 – 2016-04-04 13:08:50

2

隨着引進ContraintLayout的實施最佳方式,它是可能的準則來實現:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.eugene.test1.MainActivity"> 

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:background="#AAA" 
     android:text="TextView" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintStart_toStartOf="parent" 
     app:layout_constraintEnd_toEndOf="parent" 
     app:layout_constraintBottom_toTopOf="@+id/guideline" /> 

    <android.support.constraint.Guideline 
     android:id="@+id/guideline" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     app:layout_constraintGuide_percent="0.5" /> 

</android.support.constraint.ConstraintLayout> 

Result View

您可以在這篇文章中閱讀更多Building interfaces with ConstraintLayout