gpt4 book ai didi

android - view.requestLayout - 以编程方式设置按钮布局

转载 作者:行者123 更新时间:2023-11-29 22:39:30 27 4
gpt4 key购买 nike

已经有 some solution here关于如何以编程方式更改按钮尺寸

但是,对我有用的是:Here in this site

作者在其中做了这样的事情:

ImageButton someView = (ImageButton)findViewById(R.id.myimage);
someView.requestLayout();
someView.getLayoutParams().width = newWidth;
someView.getLayoutParams().height = newHeight;

现在我只是尝试这样做排除 someView.requestLayout() 并且它仍然有效。 I read the docs about this function但我什么也得不到(我还是个初学者)。这个函数的用途是什么?从长远来看,排除它会造成任何伤害或异常吗?

最佳答案

自行设置宽高不会触发widget的布局。如果您的代码位于 Activity 的 onCreate() 方法中,或者存在触发布局的另一段代码,则已经安排了布局传递,这将是正确的。例如,如果除了设置宽度和高度外,还设置了比例类型,则设置比例类型的方法将触发布局。

来自 ImageView.java

/**
* Controls how the image should be resized or moved to match the size
* of this ImageView.
*
* @param scaleType The desired scaling mode.
*
* @attr ref android.R.styleable#ImageView_scaleType
*/
public void setScaleType(ScaleType scaleType) {
if (scaleType == null) {
throw new NullPointerException();
}

if (mScaleType != scaleType) {
mScaleType = scaleType;

requestLayout();
invalidate();
}
}

如您所见,如果比例类型已更改,则设置比例类型会触发布局请求。

这是一个测试概念的小应用程序。简而言之,可以在初始布局之前(在 onCreate() 中)、在初始布局完成之后(在 onGlobalLayout() 中)或通过某些用户更改大小 Action (在 onClick() 中)。请参阅代码中有关应用程序工作方式的注释。

MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener,
ViewTreeObserver.OnGlobalLayoutListener {
private View theView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
theView = findViewById(R.id.view);
// If the following line execute, it is before the initial layout pass,
// so the new width will take effect during that layout pass.
// Comment and uncomment this line to see the effect.
// theView.getLayoutParams().width *= 2;

// Uncomment the following to change size after the initial layout.
theView.getViewTreeObserver().addOnGlobalLayoutListener(this);
}

@Override
public void onClick(View v) {
// theView.getLayoutParams().width *= 2;
// The layout pass has completed since the button is available to click.
// Here setting the width without requesting a layout will not change the
// width of the widget - at least not until there is a layout pass requested.
// Comment and uncomment this line to see the effect.
// theView.requestLayout();
}

@Override
public void onGlobalLayout() {
theView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
theView.getLayoutParams().width *= 2;
theView.requestLayout();
}
}

activity_main.xml

<androidx.constraintlayout.widget.ConstraintLayout 
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<View
android:id="@+id/viewStatic"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_marginBottom="16dp"
android:background="@android:color/holo_red_light"
android:text="Hello World!"
app:layout_constraintBottom_toTopOf="@+id/view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />

<View
android:id="@+id/view"
android:layout_width="100dp"
android:layout_height="50dp"
android:background="@android:color/holo_red_light"
android:text="Hello World!"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/viewStatic" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Make larger"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/view" />

</androidx.constraintlayout.widget.ConstraintLayout>

关于android - view.requestLayout - 以编程方式设置按钮布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59172710/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com