【问题标题】:Constrain the size of buttons within a linear layout to prevent pushing off screen限制线性布局内按钮的大小,以防止推离屏幕
【发布时间】:2022-02-02 21:37:23
【问题描述】:

我有一个垂直线性布局,它是scrollView 的一部分。在垂直线性布局中,我添加了水平线性布局,每个布局有 2 个按钮,一个带有名称的较大按钮和一个小的删除按钮。当名称变得太大时,删除按钮会被压扁或基本上被删除。我想通过使删除按钮始终保持相同大小并将文本按钮限制在布局中剩余的空间来避免这种情况。

这是我的保存文件,我想防止删除按钮变小。这可以通过使文本位于多行或更改字体大小来实现,但我的任何尝试都没有奏效。我的程序的所有功能都很完美,但尺寸太大,很难在多个设备上使用。

这是linearLayoutscrollViewXML

 <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


    【解决方案1】:

    为了避免左键贪图删除键,避免删除键被挤压,需要做两件事:

    • 为 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++;
    

    测试:

    【讨论】:

    • 这对我来说非常有效,非常感谢!我已经为此苦苦挣扎太久了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多