Androidアプリケーションでカスタム幅のUI要素を使用しています。 UI要素は基本的に、2つの緑色の円を持つブロックです。ブロックの左側に1つの円、右側にもう1つの円です。Android:LayoutParamsを使用してViewオブジェクトの幅を設定する
UIエレメント全体の幅をプログラムで設定したいと考えています。円は常に一定の大きさでなければならず、中央に座っているブロックは、残りのスペースを埋めるようにサイズ調整する必要があります。ここで
は、私が現在持っているものです。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<View
android:background="@drawable/green_circle"
android:id="@+id/left_green_circle"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
/>
<View
android:background="@drawable/blue_rectangle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
/>
<View
android:background="@drawable/green_circle"
android:id="@+id/right_green_circle"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
私は、このようなコードで表示のレイアウトパラメータを設定します。
int layoutLeft = block_x_position - green_circle_width;
int layoutWidth = block_width + (green_circle_width * 2);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(layoutWidth, block_height);
layoutParams.setMargins(layoutLeft, block_y_position, 0, 0);
this.setLayoutParams(layoutParams);
this.requestLayout();
私が原因で上記のコードでRelativeLayoutを使用していますこの全体のView要素の親はRelativeLayoutです。残念ながら、私は期待していた結果を得ていません。私の "block_width"変数は "40dp"で、私の "green_circle_width"変数は "20dp"です。私が代わりに私のビューのXMLで「match_parent」の「wrap_content」を使用して試してみました
、それは任意のは全く影響を与えていない:ここで私は取得していますものです。私のブロックが幅を超えて拡張されていて、それを拡張したいのであれば、どんな考えですか?
ありがとうございました。
右にいくらかのパディングはありますか? – bluejamesbond