1

我正在尝试动态创建ImageButton一个样式并对其应用样式。

这是我的风格:

<style name="ActionBarActionButton">
    <item name="android:layout_width">30dp</item>
    <item name="android:layout_height">fill_parent</item>
    <item name="android:background">@drawable/action_button</item>
</style>

这是按钮创建:

ImageButton actionButton = new ImageButton(context, null, R.style.ActionBarActionButton);

它似乎根本没有效果,没有应用样式参数。

4

1 回答 1

2

I found into that this way doesn't work, but nobody could told why (hello google?)

I had to use such workarround:

I've crated a new layout file for button:

<?xml version="1.0" encoding="utf-8"?>
<ImageButton xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="40dp"
    android:layout_height="fill_parent"
    android:background="@drawable/action_button" >
</ImageButton>

And then inflated it into layout like this:

    LayoutInflater inflater =  (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    ImageButton actionButton = (ImageButton) inflater.inflate(R.layout.imagebutton_action, getActionsLayout(), false);
    actionButton.setImageResource(action.getIconResource());        
    getActionsLayout().addView(actionButton);
于 2012-12-09T10:01:44.310 回答