TextView の幅をその親の幅と正確に等しくしたい場合は、使用できます
android:layout_width="fill_parent"
パーツの幅の半分にしたい場合は?または、一般的に、親の幅に対して相対的に幅を設定しますか?
編集:相対レイアウトを使用しています
私の画面はこんな感じです。
TextView の幅をその親の幅と正確に等しくしたい場合は、使用できます
android:layout_width="fill_parent"
パーツの幅の半分にしたい場合は?または、一般的に、親の幅に対して相対的に幅を設定しますか?
編集:相対レイアウトを使用しています
私の画面はこんな感じです。
手っ取り早い方法は次のとおりです。
<?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>
別のコンテナにラップして、親の重量合計の半分だけを使用する必要があります。
使用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" />
まず第一に、非推奨チームである "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);
}
次のように、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>
@rj と @djhacktorreborn が提案したように、TextView を Linear Layout でラップし、全体を Relative Layout に入れます。