我试图设置此活动" ChoiceList"这样,每次单击保存按钮时,都会在ChoiceList活动的scrollView中的linearLayout中添加一个新视图。添加的视图" choiceView"应该只是frameLayout中包含的textView。但是,出于某种原因,每次单击保存按钮时,都会将新的ChoiceList布局添加到linearLayout而不是ChoiceView。我是一个刚开始教我自己如何开发的业余Android开发人员,所以我不知道问题出在哪里。
以下是我的java和xml文件:
public class ChoiceList extends android.app.Activity implements android.view.View.OnClickListener {
private ArrayList<ChoiceView> choiceList;
private Button button;
private LinearLayout choiceListLayout;
private ViewGroup parentGroup;
public void onCreate(Bundle savedInstanceState) {//, List<ChoiceView> list) {
super.onCreate(savedInstanceState);
choiceList = new ArrayList<ChoiceView>();
this.getActionBar().setTitle("");
this.getActionBar().setDisplayHomeAsUpEnabled(true); // adds a left-facing caret alongside the app icon and enables it as an action button
// such that when the user presses it, the activity receives a call to
// onOptionsItemSelected(). The ID for the action is android.R.id.home.
this.initLayout();
}
public void initLayout() {
this.setContentView(R.layout.activity_choice_list); // set up initial layout
choiceListLayout = (LinearLayout) findViewById(R.id.choiceListLayout);
parentGroup = (ViewGroup) choiceListLayout;
button = (android.widget.Button) findViewById(R.id.saveButton);
button.setOnClickListener(new android.view.View.OnClickListener() { // when "Save" button is clicked
@Override
public void onClick(View v) {
newChoiceView(); // Makes new ChoiceView with text from EditText
}
});
}
/**
* Makes and displays new ChoiceView with text from EditText
* Called within InitLayout from clicking Save button
*/
public void newChoiceView() {
ChoiceView cv = new ChoiceView(this.getApplicationContext());
choiceList.add(cv); // add ChoiceView to list of ChoiceViews
choiceListLayout.addView(cv); // display ChoiceView
}
...
}
和相关的xml ...
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:id="@+id/choiceScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/choiceListLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
<EditText
android:id="@+id/enterChoice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/enterChoiceText" />
<Button
android:id="@+id/saveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/save"
android:textColor="@color/white" />
</LinearLayout>
</ScrollView>
<Button
android:id="@+id/spinButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="@string/spin" />
</RelativeLayout>
以及我尝试添加到滚动条的视图...
public class ChoiceView extends RelativeLayout {
protected TextView tv;
private String choiceString; // choice text
private EditText et; // editText from which text comes from
/**
* @param context
*/
public ChoiceView(Context context) {
super(context);
LayoutInflater choiceViewInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View choiceView = choiceViewInflater.inflate(R.layout.choice_view, this, true);
LayoutInflater choiceListInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View choiceList = choiceListInflater.inflate(R.layout.activity_choice_list, this, true);
et = (EditText) choiceList.findViewById(R.id.enterChoice); // finds EditText from which string heeds
tv = (TextView) choiceView.findViewById(R.id.choiceViewTextView); // finds TextView to display text
tv.setText(et.getText().toString());
requestLayout();
}
}
及其相关的xml ...
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/whatever" >
<TextView
android:id="@+id/choiceViewTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/spin"
android:padding="5dp" />
</FrameLayout>
如果您需要任何进一步的信息,请与我们联系。
答案 0 :(得分:1)
看起来您正在尝试动态地将元素添加到可滚动列表中,以后可以选择该列表。
Android已经提供了一个用于执行此操作的UI组件ListView,它由适配器支持,该适配器将您的模型(即数据)与UI桥接。在这种情况下,您的模型是字符串列表。适配器负责根据需要创建视图(在本例中为TextViews)。 Check out this excellent tutorial.