2

画面の幅のパーセンテージだけを占有したいレイアウトがあります (左の画像)。現在、私の相対レイアウトはディスプレイの幅全体に広がっています (右の画像)。左右に余白があるように修正するにはどうすればよいですか?

ここに画像の説明を入力

これは、相対レイアウトを定義した方法です。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:background="@color/white"
    android:orientation="vertical" 
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp">
4

2 に答える 2

6

このためyou can use LinearLayout with weightSum and layout_wright property

次のコードを試してください

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="8" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

        <!-- 1/8 % of Screen to left of your RelativeLayour -->
    </LinearLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="6"
        android:background="@color/white"
        android:orientation="vertical" >
    </RelativeLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

        <!-- 1/8 % of Screen to right of your RelativeLayour -->

    </LinearLayout>

</LinearLayout>
于 2013-08-01T18:54:34.630 に答える
1

新しいパーセンテージ サポート ライブラリを使用します。

compile 'com.android.support:percent:24.0.0'

PercentageRelativelayout を使用すると、これを行うことができます。以下の例を参照してください。

    <android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_vertical"
    android:background="@android:color/black"
    android:orientation="vertical"
    app:layout_marginLeftPercent="5%"
    app:layout_marginRightPercent="5%"
    app:layout_marginTopPercent="5%"
    app:layout_marginBottomPercent="5%"
    >
</RelativeLayout>

詳細情報Android Doc

*例とチュートリアルの場合 * Tutorial1 Tutorial2 Tutorial3

于 2016-10-26T13:26:36.447 に答える