权重在自定义Android组件中不起作用

时间:2012-09-10 21:36:25

标签: android android-layout android-custom-view

我想创建一个自定义底部按钮栏布局,我创建了一个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文件中,它可以正常工作,只有在整体使用它时才会出现问题。

P.S。 以这种方式创建组件是一个好习惯吗?或者也许我做错了什么?

谢谢, 丹

2 个答案:

答案 0 :(得分:2)

嗯,我从来没有看到过这样的布局。可能会有一些时髦的事情,请尝试这种单行方法:

//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
....

这就是我用于任何自定义组件的内容。我已经做了很多像这样附加权重的LinearLayouts,所以不应该有任何问题。使它看起来更干净,因为它将三行减少到一行。

答案 1 :(得分:0)

在addView上,在layoutWidth上传递了LinearLayout.LayoutParams和FILL_PARENT。