3

ピクセルではなく画面の幅と高さのパーセンテージに基づいて、TextView または関連要素の XML ベースの位置を調整することは可能ですか? たとえば、

android:layout-marginLeft="150dip" 

のようなものに置き換えられます

android:layout-marginLeft="25%"? 

そうでない場合、プログラムで行うことは可能ですか?

4

2 に答える 2

3

属性は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% を使用し、右がTextView75% を使用しています。

于 2013-08-27T21:38:10.963 に答える
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>
于 2016-05-09T15:44:57.230 に答える