11

TextView の幅をその親の幅と正確に等しくしたい場合は、使用できます

android:layout_width="fill_parent"

パーツの幅の半分にしたい場合は?または、一般的に、親の幅に対して相対的に幅を設定しますか?

編集:相対レイアウトを使用しています

私の画面はこんな感じです。

私の画面はこんな感じです。

4

6 に答える 6

15

手っ取り早い方法は次のとおりです。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

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

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#ff0000"
            />

    </LinearLayout>

</RelativeLayout>

別のコンテナにラップして、親の重量合計の半分だけを使用する必要があります。

于 2013-03-10T09:42:32.743 に答える
9

使用android:layout_weight="0.5"してくださいlinearLayout

<LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:weightSum="1"
                android:gravity="center"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/feedback"
                    android:layout_width="wrap_content"
                    android:layout_weight="0.5"
                    android:textSize="15pt" />
于 2013-03-10T09:19:41.413 に答える
1

まず第一に、非推奨チームである "fill_parent" を使用しないでください。"match_parent" を使用してください。

次に、半分TextViewの親を親のどこに配置しますか?

RelativeLayout を使用しているため、このアクションは少し難しくなります。次のようなコードから実行してみてください。

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
int myWidth = (int) (parentHeight * 0.5);
super.onMeasure(MeasureSpec.makeMeasureSpec(myWidth, MeasureSpec.EXACTLY),  
heightMeasureSpec);
}

参照: 親のサイズに基づいて Android ビューのサイズを変更する方法

于 2013-03-10T09:22:11.533 に答える
1

次のように、parent の weightSum を設定したい場合がありますLinearLayout:-

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center_horizontal"
    android:padding="6dp"
    android:weightSum="2">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="@string/hello"
        android:layout_weight="1" />

</LinearLayout>
于 2013-03-10T09:23:43.197 に答える
0

@rj と @djhacktorreborn が提案したように、TextView を Linear Layout でラップし、全体を Relative Layout に入れます。

于 2013-03-10T09:30:37.413 に答える