如何获取ImageButton的宽度
How to get ImageButton width
如何将在 XML 中设置的 ImageButton 的宽度设置为 0,并根据权重设置大小?
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:orientation="horizontal">
<ImageButton
android:id="@+id/fluido"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:tint="@color/icon"
android:layout_weight="0.33"
app:srcCompat="@drawable/fluido" />
<......../>
</LinearLayout>
如何获取由 weight 自动设置的这个值?
在XML中设置为0,如果我这样做:imageButton.getWidth();
或imageButton.getLayoutParams().width();
return 0 和 XML.
可以使用以下代码段:
yourView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
yourView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
yourView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
// Correct view dimensions are here:
int width = yourView.getMeasuredWidth();
int height = yourView.getMeasuredHeight();
}
});
Button button;
button = (Button) findViewById(R.id.button);
int buttonWidth = button.getWidth();
// Showing the Result
Toast.makeText(MainActivity.this, ""+buttonWidth, Toast.LENGTH_SHORT).show();
//布局图像
// 结果
它给出 0,因为您必须确保存储宽度的整数未声明为最终整数。
应该不是这样的:
Click to see Image
你应该使用 addOnPreDrawListener
view.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener()
{
@Override
public boolean onPreDraw()
{
if (view.getViewTreeObserver().isAlive())
view.getViewTreeObserver().removeOnPreDrawListener(this);
// put your code here
return false;
}
});
如何将在 XML 中设置的 ImageButton 的宽度设置为 0,并根据权重设置大小?
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:orientation="horizontal">
<ImageButton
android:id="@+id/fluido"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:tint="@color/icon"
android:layout_weight="0.33"
app:srcCompat="@drawable/fluido" />
<......../>
</LinearLayout>
如何获取由 weight 自动设置的这个值?
在XML中设置为0,如果我这样做:imageButton.getWidth();
或imageButton.getLayoutParams().width();
return 0 和 XML.
可以使用以下代码段:
yourView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
yourView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
yourView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
// Correct view dimensions are here:
int width = yourView.getMeasuredWidth();
int height = yourView.getMeasuredHeight();
}
});
Button button;
button = (Button) findViewById(R.id.button);
int buttonWidth = button.getWidth();
// Showing the Result
Toast.makeText(MainActivity.this, ""+buttonWidth, Toast.LENGTH_SHORT).show();
//布局图像
// 结果
它给出 0,因为您必须确保存储宽度的整数未声明为最终整数。
应该不是这样的: Click to see Image
你应该使用 addOnPreDrawListener
view.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener()
{
@Override
public boolean onPreDraw()
{
if (view.getViewTreeObserver().isAlive())
view.getViewTreeObserver().removeOnPreDrawListener(this);
// put your code here
return false;
}
});