2013-08-27 4 views
1

Возможно ли расположить позицию TextView или связанного с ним XML на основе процентных значений ширины и высоты экрана, а не пикселей? Например, можетAndroid - Позиционные представления, основанные на процентах, а не на пикселях

android:layout-marginLeft="150dip" 

заменить что-то вроде

android:layout-marginLeft="25%"? 

Если нет, то это можно сделать так программно?

+0

, а затем у вас есть вес в linearlayouts –

ответ

2

Атрибуты layout_margin не принимают процентное значение.

Вы ищете LinearLayout атрибут android:layout_weight, который позволяет использовать проценты, чтобы определить раскладку:

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight=".25" /> 

    <TextView 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight=".75" /> 

</LinearLayout> 

В этом примере, левый TextView использует 25% экрана и правой TextView 75%.

1

Используйте PercentRelativeLayout (http://developer.android.com/reference/android/support/percent/PercentRelativeLayout.html)

<android.support.percent.PercentRelativeLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
    <ImageView 
     app:layout_widthPercent="50%" 
     app:layout_heightPercent="50%" 
     app:layout_marginTopPercent="25%" 
     app:layout_marginLeftPercent="25%"/> 
</android.support.percent.PercentFrameLayout> 
Смежные вопросы