【发布时间】:2022-02-02 21:37:23
【问题描述】:
我有一个垂直线性布局,它是scrollView 的一部分。在垂直线性布局中,我添加了水平线性布局,每个布局有 2 个按钮,一个带有名称的较大按钮和一个小的删除按钮。当名称变得太大时,删除按钮会被压扁或基本上被删除。我想通过使删除按钮始终保持相同大小并将文本按钮限制在布局中剩余的空间来避免这种情况。
这是我的保存文件,我想防止删除按钮变小。这可以通过使文本位于多行或更改字体大小来实现,但我的任何尝试都没有奏效。我的程序的所有功能都很完美,但尺寸太大,很难在多个设备上使用。
这是linearLayout 和scrollView 的XML:
<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++;
}
}
【问题讨论】:
标签: java android android-layout android-linearlayout android-layout-weight