2013-07-23 2 views
2

Как я могу сделать некоторый макет, например, 80% или 90% ширины экрана, независимо от размера экрана или DPI? Я попытался с ", но я получаю 80% от высоты затемКак сделать макет размером 80% ширины экрана?

EDIT:..

<?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 основана на android:orientation вашего LinearLayout - если вы хотите, чтобы ваш макет занять 80% ширины, вам нужно LinearLayout с android: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

Смежные вопросы