Android が ImageButton のレイアウト パラメータを無視しているようです。私のレイアウト xml がスタイルで ImageButtons を定義するとき、layout_weight が考慮されます。ただし、ボタンを動的に追加すると、重みが無視され、LayoutParameters を手動で定義する必要があります。
Styles.xml エントリ:
<style name="TabbedPanelToolbarControl" parent="android:style/Widget.ImageButton">
<item name="android:background">@null</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_weight">1.0</item>
<item name="android:paddingTop">5dp</item>
<item name="android:paddingBottom">5dp</item>
</style>
レイアウト xml:
...
<LinearLayout
android:id="@+id/toolbar"
android:orientation="vertical" >
<!-- image buttons go here -->
</LinearLayout>
...
コード:
View view = inflater.inflate(R.layout.test, container, false);
LinearLayout mToolbar = (LinearLayout)view.findViewById(R.id.toolbar);
ImageButton ib = new ImageButton(this.getActivity(), null, R.style.TabbedPanelToolbarControl);
ib.setImageDrawable(icon);
ib.setContentDescription(title);
// ignored layout_weight defined in TabbedPanelToolbarControl style
mToolbar.addView(ib);
// need to re-specify layout
mToolbar.addView(ib, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));
ボタンを動的に追加するときに、styles.xml から layout_weight を強制的に読み取る方法を知っている人はいますか?