Android LinearLayout每行一个元素

时间:2017-05-21 10:38:29

标签: android checkbox layout

我想以编程方式在LinearLayout中每行对齐一个CheckBox元素。 我在LinearLayout对象中动态添加CheckBox元素。

我尝试使用setLines(1)setWidth(400)设置CheckBox。

public class QuizQuestionActivity extends AppCompatActivity {

    TextView questionText;
    LinearLayout answerLinearLayout;
    ArrayList<CheckBox> answers;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_quiz_question);
        setTitleQuestion("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus pellentesque pulvinar rutrum.");

        answerLinearLayout = (LinearLayout) findViewById(R.id.answerLinearLayout);
        displayAnswers();
    }

    private void setTitleQuestion(String titleQuestion) {
        setContentView(R.layout.activity_quiz_question);
        questionText = (TextView) findViewById(R.id.titleQuestionText);
        questionText.setText(titleQuestion);
    }

    private void displayAnswers() {
        createAnswers();
        for (CheckBox answer: answers) {
            answerLinearLayout.addView(answer);
        }
    }

    private void createAnswers() {
        answers = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            String content = "Réponse : "+i;
            CheckBox checkbox = createAnswer(content);
            System.out.println(checkbox);

            answers.add(checkbox);
        }
    }

    private CheckBox createAnswer(String text) {
        CheckBox answer = new CheckBox(this);

        answer.setLines(1);
        answer.setWidth(400);
        answer.setText(text);
        answer.setClickable(true);

        return answer;
    }

    private String getQuizQuestionId() {
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            return extras.getString(MainActivity.INTENT_QUIZ_QUESTION_ID);
        }

        return null;
    }
}




<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.alexbdev.quizzy.QuizQuestionActivity">


    <ScrollView
        android:id="@+id/scrollView2"
        android:layout_width="368dp"
        android:layout_height="80dp"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/titleQuestionText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:ellipsize="none"
            android:gravity="left"
            android:textStyle="normal" />
    </ScrollView>

    <LinearLayout
        android:id="@+id/answerLinearLayout"
        android:layout_width="368dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:orientation="horizontal"
        app:layout_constraintTop_toBottomOf="@+id/scrollView2"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"></LinearLayout>

</android.support.constraint.ConstraintLayout>

修改

好吧我想我需要将LinearLayout.LayoutParams作为第二个参数传递给addView函数,我已经尝试了以下代码,但只显示了第一个元素。

private void displayAnswers() {
    createAnswers();
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT
    );
    for (CheckBox answer: answers) {
        answerLinearLayout.addView(answer, params);
    }
}

1 个答案:

答案 0 :(得分:0)

请注意您的方向是:

android:orientation="horizontal"

如果你想要它是每行它应该是:

android:orientation="vertical"