I wanted to create a custom bottom button bar layout, I've created a xml file :
<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" />
as you can see I've given all the buttons width 0dp and weight of 1. then, I've created a class that extends the linear layout class :
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);
...
...
...
}
the problem is when i add this class to the main activity 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" />
the weight stops working, all the buttons are different. and I don't know why !? if I just copy paste the bottom's bar xml code to the main xml file it works fine, the problem only occurs when using it as a whole.
P.S. Is it a good practice to create a component this way? Or maybe I'm doing something wrong?
Thanks, Dan
Hmm, I've never seen a layout inflated exactly that way. There may be something funky going on with it, try this one-line approach instead:
//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
....
That's what I use for any custom components. I've done plenty of LinearLayouts with weights attached like this, so there shouldn't be any issue with that. Makes it read cleaner too, since it cuts three lines down to one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With