我不確定我是否有迴應作爲評論或作爲答案。我已經按照@AlbertoMéndez的回答,並改爲列表,因爲評論說,我認爲我得到了解決方案,我仍然需要努力使看起來更好,但結構是我想要做的: auxiliary_list.xml佈局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background_gradient">
<LinearLayout
android:layout_width="255dp"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/main_name_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title_entry"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/checkbox_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Checkbox"
android:clickable="false" />
<Switch
android:id="@+id/switch_button_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="Text off"
android:textOn="Text on"
android:clickable="false" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center">
<Button
android:layout_width="5dp"
android:layout_height="30dp"
android:id="@+id/update_entry_list"
android:background="@android:drawable/ic_menu_edit"
android:layout_weight="0.5"
android:height="10dp" />
<Button
android:layout_width="5dp"
android:layout_height="30dp"
android:id="@+id/delete_entry_list"
android:background="@android:drawable/ic_delete"
android:layout_weight="0.5"
android:height="10dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
主要activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/screen_padding"
android:orientation="vertical"
android:gravity="center_horizontal"
android:background="@drawable/background_gradient">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/title"
android:id="@+id/manage_exercises_title"
android:text="@string/title"/>
<Space
android:layout_width="match_parent"
android:layout_height="@dimen/content_spacing" />
<ListView
android:id="@+id/list_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="10dp"
android:src="@android:drawable/ic_input_add" />
</LinearLayout>
Java代碼填充有例如:
package com.example.nck.routine_tracker;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Arrays;
/**
* Created by nck on 14/10/16.
*/
public class MainJavaFile extends Activity{
ListView list;
String [] text = new String[20];
boolean [] option1 = new boolean[20];;
boolean [] option2 = new boolean[20];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity_xml);
for (int i=0;i<20;i++){
text[i]=""+ i + "º Entry";
}
list = (ListView) findViewById(R.id.list_1);
list.setAdapter(new listAdapter(text,option1,option2, this.getBaseContext()));
Toast.makeText(this, text[0],Toast.LENGTH_LONG).show();
}
}
class listAdapter extends BaseAdapter {
String[] text;
boolean[] option1, option2;
Context context;
LayoutInflater inflater;
public listAdapter() {
text = null;
option1 = null;
option2 = null;
}
public listAdapter(String[] text, boolean[] option1, boolean[] option2, Context context) {
this.text = text;
this.option1 = option1;
this.option2 = option2;
this.context= context;
}
public int getCount() {
// TODO Auto-generated method stub
return text.length;
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row;
row = inflater.inflate(R.layout.auxiliary_list, parent, false);
TextView Text;
CheckBox Option1;
Switch Option2;
Text = (TextView) row.findViewById(R.id.main_name_list);
Option1 = (CheckBox) row.findViewById(R.id.checkbox_list);
Option2 = (Switch) row.findViewById(R.id.switch_button_list);
Text.setText(text[position]);
Option1.setChecked(option1[position]);
Option2.setEnabled(option2[position]);
return (row);
}
}
而且該解決方案:
來源
2016-10-14 19:27:21
nck
由於意見都在重複,我相信,這使你把你的數據在[ListView控件]更有意義(https://developer.android.com/reference/android/widget /ListView.html)。 – MohanadMohie
或'RecyclerView' => https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html – xAqweRx