简体   繁体   English

限制线性布局内按钮的大小,以防止推离屏幕

[英]Constrain the size of buttons within a linear layout to prevent pushing off screen

I have a vertical linear layout that is a part of a scrollView .我有一个垂直线性布局,它是scrollView的一部分。 In the vertical linear layout I add horizontal linear layouts with 2 buttons in each, one larger button with a name and a small delete button.在垂直线性布局中,我添加了水平线性布局,每个布局有 2 个按钮,一个带有名称的较大按钮和一个小的删除按钮。 When the name becomes too big the delete buttons gets squished or essentially removed.当名称变得太大时,删除按钮会被压扁或基本上被删除。 I want to avoid this by causing the delete button to always remain the same size and constraining the text button to the space remaining in the layout.我想通过使删除按钮始终保持相同大小并将文本按钮限制在布局中剩余的空间来避免这种情况。

Here is my save files, I want to prevent the delete button from getting any smaller.这是我的保存文件,我想防止删除按钮变小。 This could be either achieved by making the text be on multiple lines, or changing the font size, but none of my attempts have worked as of yet.这可以通过使文本位于多行或更改字体大小来实现,但我的任何尝试都没有奏效。 All the functionality of my program is perfect, but the sizing is making it difficult to have on multiple devices.我的程序的所有功能都很完美,但是尺寸很难在多个设备上使用。

Here is the XML for the linearLayout and scrollView :这是用于XMLscrollView linearLayout

 <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>

And Here is the code for adding the buttons:这是添加按钮的代码:

    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++;
        }
    }

In order to avoid the left button of being greedy on the delete button and avoid the delete button to be squeezed, you need a couple of things:为了避免左键贪图删除键,避免删除键被挤压,需要做两件事:

  • Add a layout weight to the Button greater than 0.给 Button 添加一个大于 0 的布局权重。
  • Set the width of the horizontal LinearLayout to WRAP_CONTENT将水平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++;

Testing that:测试:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM