我有一個垂直線性布局,它是scrollView. 在垂直線性布局中,我添加了水平線性布局,每個布局有 2 個按鈕,一個帶有名稱的較大按鈕和一個小的洗掉按鈕。當名稱變得太大時,洗掉按鈕會被壓扁或基本上被洗掉。我想通過使洗掉按鈕始終保持相同大小并將文本按鈕限制在布局中剩余的空間來避免這種情況。

這是我的保存檔案,我想防止洗掉按鈕變小。這可以通過使文本位于多行或更改字體大小來實作,但我的任何嘗試都沒有奏效。我的程式的所有功能都很完美,但是尺寸很難在多個設備上使用。
這是XMLforlinearLayout和scrollView:
<ScrollView
android:id="@ id/loadScrollView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="32dp"
app:layout_constraintBottom_toTopOf="@ id/returnToMainButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/loadGameText"
app:layout_constraintVertical_bias="1.0">
<LinearLayout
android:id="@ id/loadLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_marginTop="2dp"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
這是添加按鈕的代碼:
public void addButtons() {
LinearLayout linearLayout = findViewById(R.id.loadLinearLayout);
int i = 0;
for(File file : files) {
LinearLayout innerLayout = new LinearLayout(this);
innerLayout.setOrientation(LinearLayout.HORIZONTAL);
Button newButton = new Button(this);
String filename = file.getName();
newButton.setId(i);
newButton.setText(filename);
newButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
loadGame(view);
}
});
innerLayout.addView(newButton);
ImageButton deleteButton = new ImageButton(this);
deleteButton.setImageResource(android.R.drawable.ic_menu_delete);
deleteButton.setId(500 i);
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
deleteSave(view);
}
});
innerLayout.addView(deleteButton);
linearLayout.addView(innerLayout);
i ;
}
}
uj5u.com熱心網友回復:
為了避免左鍵貪圖洗掉鍵,避免洗掉鍵被擠壓,需要做兩件事:
- 給 Button 添加一個大于 0 的布局權重。
- 將水平線的寬度設定
LinearLayout為WRAP_CONTENT
LinearLayout innerLayout = new LinearLayout(this);
innerLayout.setOrientation(LinearLayout.HORIZONTAL);
Button newButton = new Button(this);
String filename = file.getName();
newButton.setId(i);
newButton.setText(filename);
newButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
loadGame(view);
}
});
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 1f); // layout_weight of 1
innerLayout.addView(newButton, params);
ImageButton deleteButton = new ImageButton(this);
deleteButton.setImageResource(android.R.drawable.ic_menu_delete);
deleteButton.setId(500 i);
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
deleteSave(view);
}
});
innerLayout.addView(deleteButton);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, // WRAP_CONTENT the widtj
LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.addView(innerLayout, layoutParams);
i ;
測驗:

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/426329.html
標籤:爪哇 安卓 安卓布局 安卓线性布局 android-layout-weight
