2013-07-23 69 views
2

無論屏幕尺寸或DPI是多少,我怎樣才能使一些佈局成爲例如屏幕寬度的80%或90%?我試着用「,但我得到了高度則有80%如何使佈局成爲屏幕寬度的80%?

編輯:。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="@drawable/background" > 

    <Button 
     android:id="@+id/bNivoi1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_marginTop="80dp" 
     android:background="@drawable/button_nivoi_back" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:weightSum="1" 
     android:orientation="horizontal" > 

    <TextView 
     android:id="@+id/tvOpis15Nivoa" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:gravity="center" 
     android:textSize="15sp" 
     android:layout_weight="0.8" 
     android:text="text" /> 
    </LinearLayout> 

</LinearLayout> 

我只需要TextView的是在屏幕的80%,沒有活動的其餘

+0

回答@ ianhanniballake的回答,將你現有的佈局封裝在一個帶水平方向的LinearLayout中(你不需要明確地指定它,默認是水平的),然後使用'android:layout_weight =「0.8」'。 – Vikram

+0

不知何故,它不工作。讓我編輯我的帖子並粘貼我的XML。 – marjanbaz

回答

8

android:layout_weight是根據你LinearLayoutandroid:orientation - 如果你希望你的版面佔用寬度的80%,你需要一個LinearLayoutandroid:orientation="horizontal"

<LinearLayout 
    android:id="@+id/your_outer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > <!-- note that vertical is the default --> 

    <!-- Other elements here --> 

    <LinearLayout 
     android:id="@+id/eighty_percent_layout_holder 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:weightSum="1"> 
     <View 
      android:id="@+id/your_eighty_percent_layout" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.8" /> 
    </LinearLayout> 

    <!-- Other elements here --> 
</LinearLayout> 
+0

謝謝。我會嘗試。 – marjanbaz

+0

@marjanbaz - 如果您仍然遇到問題,我已經添加了一些示例XML。 – ianhanniballake

+0

我做了你所做的一切,但仍然沒有。仍然100%。我編輯了我的第一篇文章。 – marjanbaz

相關問題