我想创建一个看起来像这样的Android页面

时间:2017-06-04 20:08:45

标签: android

我是Android初学者。我想创建一个Android页面,它将动态显示三件事1.问题2.你的答案3.正确答案。我怎样才能做到这一点? It should look like this and can grow dynamically

1 个答案:

答案 0 :(得分:1)

这样的事情(使用listview):

Mainactivity:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private ArrayList<Question> questionArrayList;

    private void fillList(){
        questionArrayList = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            questionArrayList.add(new Question(i,"myans","yourans"));
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fillList();
        ListView mlIstView = (ListView)findViewById(R.id.mListview);

        QuestionAdapter adapter = new QuestionAdapter(this,R.layout.listviewlayout,questionArrayList);
        mlIstView.setAdapter(adapter);
    }
}

Question.java

public class Question {
private int numberOfQuestion;
private String yourAnswer;
private String correctAnswer;

public Question(int numberOfQuestion, String yourAnswer, String correctAnswer) {
    this.numberOfQuestion = numberOfQuestion;
    this.yourAnswer = yourAnswer;
    this.correctAnswer = correctAnswer;
}

public int getNumberOfQuestion() {
    return numberOfQuestion;
}

public String getYourAnswer() {
    return yourAnswer;
}

public String getCorrectAnswer() {
    return correctAnswer;
}
}

QuestionAdapter

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by waro on 6/4/17.
 */

public class QuestionAdapter extends ArrayAdapter<Question> {
    private ArrayList<Question> arrayList;
    public QuestionAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull ArrayList<Question> objects) {
        super(context, resource, objects);
        arrayList = objects;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        if(convertView == null){
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.listviewlayout,parent,false);
        }
        Question question = arrayList.get(position);
        TextView questiontv = (TextView)convertView.findViewById(R.id.question);
        TextView yourans = (TextView)convertView.findViewById(R.id.yourans);
        TextView correctans = (TextView)convertView.findViewById(R.id.correctans);

        questiontv.setText(Integer.toString(question.getNumberOfQuestion()));
        yourans.setText(question.getYourAnswer());
        correctans.setText(question.getCorrectAnswer());
        return convertView;
    }
}

activity_mail.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.suprentive.waro.myapplication.MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="top"
        android:id="@+id/mLinearLayout">
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Question"
            android:layout_weight="1"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Your answer"
            android:layout_weight="1"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Right answer"
            android:layout_weight="1"/>
    </LinearLayout>
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/mListview"
        android:layout_below="@id/mLinearLayout">
    </ListView>
</RelativeLayout>

listviewlayout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text=""
        android:id="@+id/question"
        android:layout_weight="1"/>
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text=""
        android:id="@+id/yourans"
        android:layout_weight="1"/>
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text=""
        android:id="@+id/correctans"
        android:layout_weight="1"/>
</LinearLayout>

结果Result

希望这会帮助你。