gpt4 book ai didi

android - 如何创建完全填充屏幕宽度的方形按钮

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:04:31 25 4
gpt4 key购买 nike

我有一个基于 TableLayout 和 TableRow 动态填充一些按钮的 Activity ,如下所示:

    private TableLayout buttonTableLayout;
//-----
for (int row = 0; row < buttonTableLayout.getChildCount(); ++row)
((TableRow) buttonTableLayout.getChildAt(row)).removeAllViews();

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

for (int row = 0; row < 5; row++) {
TableRow currentTableRow = getTableRow(row);

for (int column = 0; column < 5; column++) {
Button newGuessButton = (Button) inflater.inflate(R.layout.my_button, currentTableRow, false);
newGuessButton.setText(String.valueOf((row * 5) + column + 1));
currentTableRow.addView(newGuessButton);
}

}
}
//----
private TableRow getTableRow(int row) {
return (TableRow) buttonTableLayout.getChildAt(row);
}

我想制作一个 5*5 的按钮列表,1:所有按钮都具有相同的宽度和高度,2:让它们填满屏幕。在我的代码中,我有一个名为 my_button 的布局,例如:

<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/newButton"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="@drawable/button_style"></Button>

<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/newButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/button_style"></Button>

结果是: enter image description here enter image description here

我已经改变了重力但它不起作用。有什么办法可以使它们完全正方形并填充屏幕的宽度。

最佳答案

2022 年更新:

现在我们可以使用 ConstraintLayoutapp:layout_constraintDimensionRatio="1:1" 轻松完成。这是 the google guide link.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:text="My Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

上一个答案:您应该将 Button View 子类化并覆盖以下函数:

public class SquareButton extends Button {

public SquareButton(Context context) {
super(context);
}

public SquareButton(Context context, AttributeSet attrs) {
super(context, attrs);
}

public SquareButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

public SquareButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
int size = width > height ? height : width;
setMeasuredDimension(size, size); // make it square
}

}

编辑:好的,您需要像上面那样自定义您的按钮。然后,您可以使用上面的 SquareButton,而不是使用默认按钮。

 <com.kingfisher.utils.SquareButton
android:id="@+id/btnSearch"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="100"
android:text="Downloaded"/>

关于android - 如何创建完全填充屏幕宽度的方形按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37783598/

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