[android的滚动视图中的2x2按钮

问题描述 投票:0回答:1

我正在尝试将按钮矩阵(2 x 2)放入约束布局,然后将约束(包括4个按钮)布局放入滚动视图,最后将滚动视图添加到主布局中。下面提供了代码。谁能告诉我我在做什么,因为最后出现了条形图而不是按钮矩阵?计划有4个可见按钮,但实际上出现了2个按钮。有什么建议可以使任务变得更聪明。预先谢谢!


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ConstraintLayout layout = findViewById(R.id.layout);

        ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(200, 200);
        ConstraintLayout constraintLayout = new ConstraintLayout(MainActivity.this);
        constraintLayout.setLayoutParams(params);

        GradientDrawable shape1 = new GradientDrawable();
        shape1.setColor(Color.BLUE);

        GradientDrawable shape2 = new GradientDrawable();
        shape2.setColor(Color.GREEN);

        Button button11 = new Button(MainActivity.this);
        Button button12 = new Button(MainActivity.this);
        Button button21 = new Button(MainActivity.this);
        Button button22 = new Button(MainActivity.this);

        ConstraintLayout.LayoutParams params01 = new ConstraintLayout.LayoutParams(100,100);
        button11.setLayoutParams(params01);
        button11.setX(0);
        button11.setY(0);
        constraintLayout.addView(button11);

        ConstraintLayout.LayoutParams params02 = new ConstraintLayout.LayoutParams(100,100);
        button12.setLayoutParams(params02);
        button12.setX(100);
        button12.setY(0);
        constraintLayout.addView(button12);

        ConstraintLayout.LayoutParams params03 = new ConstraintLayout.LayoutParams(100,100);
        button21.setLayoutParams(params03);
        button21.setX(0);
        button21.setY(100);
        constraintLayout.addView(button21);

        ConstraintLayout.LayoutParams params04 = new ConstraintLayout.LayoutParams(100,100);
        button22.setLayoutParams(params04);
        button22.setX(100);
        button22.setY(100);
        constraintLayout.addView(button22);

        ScrollView SV = new ScrollView(MainActivity.this);
        ConstraintLayout.LayoutParams SVparams = new ConstraintLayout.LayoutParams(300,300);
        SV.setLayoutParams(SVparams);

        constraintLayout.setBackground(shape1);
        SV.setBackground(shape2);

        SV.addView(constraintLayout);
        layout.addView(SV);

    }
<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
</androidx.constraintlayout.widget.ConstraintLayout> 

the wrong result picture (2 buttons visible instead of 4)

android scroll scrollview
1个回答
0
投票

如果您的目标实际上只是简单的2x2按钮网格(矩阵),那么我想知道为什么您需要动态地实现这一点,因为您可以只用xml描述所有布局,即使它只是一些更复杂的片段查看或说一个列表/网格项;此外,您已经具有描述ConstraintLayout的xml。因此,从角度看,它可能看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="300dp"
  android:layout_height="300dp"
  android:background="#8FFF0E">

  <androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#105BFF">

    <Button
        android:id="@+id/btnFirst"
        android:layout_width="100dp"
        android:layout_height="100dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <Button
        android:id="@+id/btnSecond"
        android:layout_width="100dp"
        android:layout_height="100dp"
        app:layout_constraintStart_toEndOf="@id/btnFirst"
        app:layout_constraintTop_toTopOf="parent"/>

    <Button
        android:id="@+id/btnThird"
        android:layout_width="100dp"
        android:layout_height="100dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/btnFirst"/>

    <Button
        android:id="@+id/btnFourth"
        android:layout_width="100dp"
        android:layout_height="100dp"
        app:layout_constraintStart_toEndOf="@id/btnThird"
        app:layout_constraintTop_toBottomOf="@id/btnSecond"/>

  </androidx.constraintlayout.widget.ConstraintLayout>

</ScrollView>

在全部视图中都准备好后,您可以按id来操作按钮(隐藏/显示它们或更改其属性)。>>

根据主题-您显然可以在ConstraintSet的帮助下以编程方式进行操作。选中此一项:ConstraintLayout: change constraints programmatically

© www.soinside.com 2019 - 2024. All rights reserved.