I am trying to creating a bottom toolbar widget that dynamically adds
buttons. Right now the buttons are simple TextViews. My problem lies when I
add the button, it fails to fill the parent in width and height of the
toolbar. Instead it simply wraps the TextView content. I don't want that. I
want it to fill the toolbar and leave no empty space.
*
Custom widget class:*
public class Toolbar extends LinearLayout
//implements View.OnClickListener
{
private LinearLayout toolbar_layout;
private LayoutInflater layout_inflater;
public Toolbar(Context context, AttributeSet attrs)
{
super(context, attrs);
// Get the layout inflater from the specified context
layout_inflater = (LayoutInflater) context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
View view = layout_inflater.inflate(R.layout.toolbar, null);
addView(view);
toolbar_layout = (LinearLayout) view.findViewById(R.id.toolbar);
}
public void addItem(String title)
{
final int index = toolbar_layout.getChildCount();
addItem(title, index);
}
public void addItem(String title, int index)
{
toolbar_layout.addView(inflateItem(title), index);
}
public void removeAllItems()
{
toolbar_layout.removeAllViews();
}
public void removeItemAt(int index)
{
toolbar_layout.removeViewAt(index);
}
public int getItemCount()
{
return toolbar_layout.getChildCount();
}
private View inflateItem(String title)
{
View view;
TextView titleView;
view = layout_inflater.inflate(R.layout.toolbar_item, toolbar_layout,
false);
titleView = (TextView) view.findViewById(R.id.item_title);
titleView.setText(title);
view.setTag(title);
//view.setOnClickListener(this);
return view;
}
}
*toolbar.xml:*
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="@dimen/toolbar_height"
android:layout_centerVertical="true"
android:padding="0dp"
android:orientation="horizontal"
android:background="@color/separator" >
</LinearLayout>
*toolbar_item.xml*
<?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="@dimen/toolbar_height"
android:layout_weight="1"
android:background="@drawable/action_bar_button"
android:clickable="true" >
<TextView
android:id="@+id/item_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_gravity="center_horizontal|center_vertical"
android:singleLine="true" />
</LinearLayout>
Inside main.xml:
<com.android.example.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="@dimen/toolbar_height"
android:orientation="horizontal"
android:padding="2dp"
android:background="@color/separator" />
This is code is roughly based on how to create a custom ActionBar from:
https://github.com/johannilsson/android-actionbar.
The toolbar item will adjust its size appropriately if I specify its size,
but fails to do so if I write "match_parent". I can't seen to figure out why
this is. Any suggestions?
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en