3

カスタムの下部ボタンバーレイアウトを作成したかったので、xmlファイルを作成しました:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@android:style/ButtonBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="0dp" >

<Button
    android:id="@+id/media_menu_button"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_margin="0dp"
    android:layout_weight="1"
    android:text="@string/media_menu_button" />

<Button
    android:id="@+id/scenario_menu_button"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_margin="0dp"
    android:layout_weight="1"
    android:text="@string/scenario_menu_button" />

 <Button
    android:id="@+id/rooms_menu_button"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_margin="0dp"
    android:layout_weight="1"
    android:text="@string/rooms_menu_button" />

<Button
    android:id="@+id/shortcut_menu_button"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_margin="0dp"
    android:layout_weight="1"
    android:text="@string/shortcut_menu_button" />

ご覧のとおり、すべてのボタンの幅を0dp、重みを1に設定しました。次に、線形レイアウトクラスを拡張するクラスを作成しました。

public class BeLightBottomBar extends LinearLayout implements OnClickListener {
private LayoutInflater mInflater; 
private Context contexnt;

private Button mShortcutMenuButton;

private Button mRoomsMenuButton;

private Button mScenarioMenuButton;

private Button mMediaMenuButton;

public BeLightBottomBar(Context context, AttributeSet attrs) {
    super(context, attrs);

    //inflate the view
    this.contexnt = context;
    mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    LinearLayout barView = (LinearLayout) mInflater.inflate(R.layout.belight_bottombar, null);
    addView(barView);

    //get all the instances of the components of the bar
    mShortcutMenuButton = (Button) barView.findViewById(R.id.shortcut_menu_button);
    mRoomsMenuButton = (Button) barView.findViewById(R.id.rooms_menu_button);
    mScenarioMenuButton = (Button) barView.findViewById(R.id.scenario_menu_button);
    mMediaMenuButton = (Button) barView.findViewById(R.id.media_menu_button);

    //set this as a click listener
    mShortcutMenuButton.setOnClickListener(this);
    mRoomsMenuButton.setOnClickListener(this);
    mScenarioMenuButton.setOnClickListener(this);
    mMediaMenuButton.setOnClickListener(this);
    ... 
    ...
    ...
  }

問題は、このクラスをメインアクティビティxmlに追加するときです

<belight.homecontrol.components.BeLightBottomBar
    android:id="@+id/button_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_margin="0dp"
    android:padding="0dp" />

重りが機能しなくなり、すべてのボタンが異なります。なぜかわからない!?下部のバーのxmlコードをメインのxmlファイルにコピーして貼り付けるだけで正常に機能する場合、問題は全体として使用する場合にのみ発生します。

PSこの方法でコンポーネントを作成するのは良い習慣ですか?それとも私は何か間違ったことをしているのでしょうか?

ありがとう、ダン

4

2 に答える 2

2

うーん、レイアウトがそのように膨らんでいるのを見たことがありません。ファンキーなことが起こっている可能性があります。代わりに、この1行のアプローチを試してください。

//inflate the view
this.contexnt = context;
LayoutInflater.from(context).inflate(R.layout.belight_bottombar, this);

//get all the instances of the components of the bar
....

これは、私がカスタムコンポーネントに使用するものです。このようにウェイトを付けたLinearLayoutをたくさん作成したので、問題はないはずです。3行を1行に削減するため、読みやすくなります。

于 2012-09-11T02:02:37.323 に答える
0

addViewで、layoutWidthにFILL_PARENTを指定してLinearLayout.LayoutParamsを渡します。

于 2012-09-10T22:00:16.540 に答える