4

RelativeLayoutボタンがあり、テキストの下にあります。ボタンのテキストに従ってテキストを揃えたい、つまり、ボタンのテキストのように左から同じ幅で開始するようにします。

使用してみandroid:layout_alignLeft="@id/myButton"ましたが、ボタンのテキストではなく、ボタンの端に揃えられます。

これを達成する方法はありますか?

私のレイアウト XML では、ボタンとテキストは次のようになります。

<Button
    android:id="@+id/myButton"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="16dp"
    android:layout_marginLeft="16dp"
    android:text="Some text" />

<TextView
    android:id="@+id/myTextView"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignLeft="@id/myButton"
    android:layout_below="@id/myButton"
    android:layout_marginTop="16dp"
    android:layout_marginLeft="16dp"
    android:text="Some text"
    android:textAppearance="?android:attr/textAppearanceMedium" />
4

4 に答える 4

1

このフィールドandroid:gravity="center"をテキストビュー コンテンツに使用してください。あなたは同じものを手に入れます..それはうまくいきます..これを使ってください

于 2013-10-19T04:59:19.340 に答える
0

コードからプログラムで実行する必要があります。コードは次のとおりです。

RelativeLayout.LayoutParams rParams = ((TextView)findViewById(R.id.myTextView)).getLayoutParams();
rParams.addRule(RelativeLayout.ALIGN_LEFT, R.id.myButton); // Or you can also add other rules like -- RelativeLayout.ALIGN_BASELINE or whatever you want to do. Check our Relative layout API docs.
((TextView)findViewById(R.id.myTextView)).setLayoutParams(rParams);

マージンとパディング、およびテキストの配置も取得できます..

((TextView))findViewById(R.id.myTextView)).setTextAlignment((Button))findViewById(R.id.myButton).getTextAlignment));

// 上記の代替コード

myTextView.setTextAlignment(myButton.getTextAlignment());

同じことがマージンとパディングにも当てはまります。

于 2013-10-19T10:56:22.723 に答える
0

XMLでどのように行うかはわかりませんが、プログラムで試してみます

myTextView.setPadding(myButton.getPaddingLeft(),0,0,0)

編集:または、ボタンとテキストビューのパディングをxmlで設定することもできます

于 2013-10-18T14:14:33.533 に答える