Inflating Image Button 以编程方式将宽度和高度参数更改为 wrap_content。为什么?
Inflating ImageButton programatically changes width & height params to wrap_content. Why?
在我的应用程序中,我使用 android.support.v7.widget.Toolbar
。
工具栏的左半部分始终充当后退按钮。
不同活动的右半部分不同。有时它只是文本,有时是一个按钮。所以我以编程方式用视图膨胀右半部分。
工具栏
<android.support.v7.widget.Toolbar>
...
... <LinearLayout
android:id="@+id/left_LL"/>
...
<LinearLayout
android:id="@+id/right_LL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:orientation="horizontal" />
</android.support.v7.widget.Toolbar>
我在 right_LL 中夸大了这个视图:
child_button.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageButton xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/right_IB"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/save"
android:background="@null"
android:onClick="onRightHeaderButton"
android:scaleType="fitXY" />
我像这样以编程方式添加它:
right_LL.addView(inflater.inflate(R.layout.child_button,
null, false));
所以问题是按钮占据了半个屏幕。它的原始尺寸是 512x512。
我想也许 LayoutParams 正在以某种方式重置,所以我也尝试了
final float scale = getResources().getDisplayMetrics().density;
int pixels = (int) (30 * scale + 0.5f);
right_IB = (ImageButton) right_LL.findViewById(R.id.right_IB);
right_IB.setLayoutParams(new ViewGroup.LayoutParams(
pixels,pixels));
它给出了这个例外:
java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams
任何帮助将不胜感激。
确保导入正确 LayoutParams
。
android.widget.LinearLayout.LayoutParams
对于 LinearLayout
或者您的 LayoutParams
导入语句可能正在导入 ViewGroup.LayoutParams
.
尝试明确使用
LinearLayout.LayoutParams(...):
spinner1.setLayoutParams(new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
当您调用 inflater.inflate(R.layout.child_button, null, false)
时,您正在为父视图传入 null
- 这会导致任何 layout_
属性被忽略(因为这些属性仅由父视图使用查看)- 请参阅 this Google+ post 了解更多详细信息。
相反,您应该使用
inflater.inflate(R.layout.child_button, right_LL, true));
这会导致您的 child_button
布局针对其父布局 right_LL
正确膨胀并自动添加(这就是 true
所做的)。
偶然的机会,我决定用 LinearLayout 围绕我的 ImageButton 并且成功了!!!
这样问题就解决了。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:id="@+id/right_IB"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@null"
android:onClick="onRightHeaderButton"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher" />
</LinearLayout>
我真的不知道为什么,但确实如此。如果有人能给我正确的解释,我会很棒。
在我的应用程序中,我使用 android.support.v7.widget.Toolbar
。
工具栏的左半部分始终充当后退按钮。
不同活动的右半部分不同。有时它只是文本,有时是一个按钮。所以我以编程方式用视图膨胀右半部分。
工具栏
<android.support.v7.widget.Toolbar>
...
... <LinearLayout
android:id="@+id/left_LL"/>
...
<LinearLayout
android:id="@+id/right_LL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:orientation="horizontal" />
</android.support.v7.widget.Toolbar>
我在 right_LL 中夸大了这个视图:
child_button.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageButton xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/right_IB"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/save"
android:background="@null"
android:onClick="onRightHeaderButton"
android:scaleType="fitXY" />
我像这样以编程方式添加它:
right_LL.addView(inflater.inflate(R.layout.child_button,
null, false));
所以问题是按钮占据了半个屏幕。它的原始尺寸是 512x512。
我想也许 LayoutParams 正在以某种方式重置,所以我也尝试了
final float scale = getResources().getDisplayMetrics().density;
int pixels = (int) (30 * scale + 0.5f);
right_IB = (ImageButton) right_LL.findViewById(R.id.right_IB);
right_IB.setLayoutParams(new ViewGroup.LayoutParams(
pixels,pixels));
它给出了这个例外:
java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams
任何帮助将不胜感激。
确保导入正确 LayoutParams
。
android.widget.LinearLayout.LayoutParams
对于 LinearLayout
或者您的 LayoutParams
导入语句可能正在导入 ViewGroup.LayoutParams
.
尝试明确使用
LinearLayout.LayoutParams(...):
spinner1.setLayoutParams(new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
当您调用 inflater.inflate(R.layout.child_button, null, false)
时,您正在为父视图传入 null
- 这会导致任何 layout_
属性被忽略(因为这些属性仅由父视图使用查看)- 请参阅 this Google+ post 了解更多详细信息。
相反,您应该使用
inflater.inflate(R.layout.child_button, right_LL, true));
这会导致您的 child_button
布局针对其父布局 right_LL
正确膨胀并自动添加(这就是 true
所做的)。
偶然的机会,我决定用 LinearLayout 围绕我的 ImageButton 并且成功了!!! 这样问题就解决了。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:id="@+id/right_IB"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@null"
android:onClick="onRightHeaderButton"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher" />
</LinearLayout>
我真的不知道为什么,但确实如此。如果有人能给我正确的解释,我会很棒。