以编程方式将控件添加到Android中的特定屏幕位置
我从安卓开始,试着开发一个小应用程序,让用户输入最多8个玩家的名字,然后点击一个按钮就可以添加更多的名字。我定义了5个标准EditText字段和2个按钮,如下所示:以编程方式将控件添加到Android中的特定屏幕位置,android,android-layout,Android,Android Layout,我从安卓开始,试着开发一个小应用程序,让用户输入最多8个玩家的名字,然后点击一个按钮就可以添加更多的名字。我定义了5个标准EditText字段和2个按钮,如下所示: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/mainLayout" and
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".AddNamesActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/player1"
android:hint="Player 1"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/player2"
android:hint="Player 2"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/player3"
android:hint="Player 3"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/player4"
android:hint="Player 4"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/player5"
android:hint="Player 5"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/addPlayerBtn"
android:text="Add Player"
android:onClick="addPlayerButtonClick"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/pickTeamsBtn"
android:text="Pick Teams"/>
</LinearLayout>
如果要在有多个控件时自动添加滚动,则必须在滚动视图中放置LinearLayout。 至于要添加的位置,您可以写:
layout.addView(newPlayer,0); // Second argument specifies index. 0 means at start
对于滚动,您可以从这里获得参考。对于添加到列表视图,根据我在android中的知识,使用SharedReference存储列表中的旧值和最新值,然后单击Add按钮尝试从Sharepreference中扩大列表
layout.addView(newPlayer,0); // Second argument specifies index. 0 means at start