我是 Android 新手,所以过去几天我一直在尝试调整,但被困在这一部分。对于我的应用程序,我正在尝试输入一个“添加更多”按钮,它会放大视图以帮助合并用户输入,但我遇到了问题。我环顾四周,使用他的源代码来帮助解决我的问题,但我遇到了一个问题,即我的膨胀 xml 布局与我之前的设置布局重叠。
这是我为 new_class_container 文件设置的布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parentView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:orientation="vertical"
android:background="@drawable/gradient_gray">
<!-- Class Name -->
<TextView
android:id="@+id/register_class_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/write_class_name"
android:layout_marginTop="20dp"
android:layout_marginLeft="5dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF"/>
<EditText
android:id="@+id/register_class_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/register_class_textview"
android:layout_alignBaseline="@+id/register_class_textview"
android:inputType="text"
android:hint="@string/example_name"
android:singleLine="true" >
</EditText>
<!-- Assignment Type and Grade Weight -->
<TextView
android:id="@+id/register_assign_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/register_class_textview"
android:layout_alignLeft="@+id/register_class_textview"
android:text="@string/write_assignment_type"
android:layout_marginTop="10dp"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<EditText
android:id="@+id/register_assign_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/register_assign_textview"
android:layout_alignLeft="@+id/register_assign_textview"
android:gravity="center_horizontal"
android:hint="@string/example_assign"
android:singleLine="true">
</EditText>
<!-- Save Button -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:gravity="bottom" >
<Button
android:id="@+id/button_save"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/save_class" />
</RelativeLayout>
<Button
android:id="@+id/btnAddNewItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/register_assign_edit"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp"
android:gravity="center_horizontal"
android:onClick="onAddNewClicked"
android:paddingLeft="5dp"
android:text="@string/more_assign"
android:textColor="#FFFFFF" />
<EditText
android:id="@+id/register_weight_edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/btnAddNewItem"
android:layout_alignLeft="@+id/register_weight_textview"
android:ems="10"
android:hint="@string/example_weight"
android:inputType="number"
android:singleLine="true" >
</EditText>
<TextView
android:id="@+id/register_weight_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/register_assign_textview"
android:layout_alignBottom="@+id/register_assign_textview"
android:layout_alignParentRight="true"
android:text="@string/write_assign_percent"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
这就是上面代码的布局的样子,我希望能够按下“添加更多”按钮,它会膨胀以“创建新行”,例如,分配类型和等级权重的 TextViews 和 EditTexts各自的TextViews。但是当我运行应用程序并按添加更多时,新的一组 TextView + EditText 会弹出我所拥有的顶部而不是底部。以下是我的另一个xml文件new_class
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/register_assign_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/write_assignment_type"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<TextView
android:id="@+id/register_weight_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/write_assign_percent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginLeft="30dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/register_assign_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:hint="@string/example_assign"
android:singleLine="true">
</EditText>
<EditText
android:id="@+id/register_weight_edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/example_weight"
android:layout_marginLeft="30dp"
android:gravity="center_horizontal"
android:inputType="number"
android:singleLine="true">
</EditText>
</LinearLayout>
</LinearLayout>
这是我拥有的 Java 代码
public class NewClass extends Activity{
private RelativeLayout mContainerView;
private Button mAddButton;
private View mExclusiveEmptyView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_class_container);
mContainerView = (RelativeLayout) findViewById(R.id.parentView);
mAddButton = (Button) findViewById(R.id.btnAddNewItem);
}
protected void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
//TODO: Handle Screen Rotation
}
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// TODO: Handle screen rotation:
// restore the saved items and inflate each one with inflateEditRow;
}
// onClick handler for the "Add new" button;
public void onAddNewClicked(View v) {
// Inflate a new row and hide the button self.
inflateEditRow(null);
v.setVisibility(View.GONE);
}
// onClick handler for the "X" button of each row
public void onDeleteClicked(View v) {
// remove the row by calling the getParent on button
mContainerView.removeView((View) v.getParent());
}
private void inflateEditRow(String name) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.new_class, null);
//final ImageButton deleteButton = (ImageButton) rowView
// .findViewById(R.id.buttonDelete);
final EditText editText = (EditText) rowView
.findViewById(R.id.register_assign_edit);
if (name != null && !name.isEmpty()) {
editText.setText(name);
} else {
mExclusiveEmptyView = rowView;
//deleteButton.setVisibility(View.INVISIBLE);
}
// A TextWatcher to control the visibility of the "Add new" button and
// handle the exclusive empty view.
editText.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
if (s.toString().isEmpty()) {
mAddButton.setVisibility(View.GONE);
//deleteButton.setVisibility(View.INVISIBLE);
if (mExclusiveEmptyView != null
&& mExclusiveEmptyView != rowView) {
mContainerView.removeView(mExclusiveEmptyView);
}
mExclusiveEmptyView = rowView;
} else {
if (mExclusiveEmptyView == rowView) {
mExclusiveEmptyView = null;
}
mAddButton.setVisibility(View.VISIBLE);
// deleteButton.setVisibility(View.VISIBLE);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
});
// Inflate at the end of all rows but before the "Add new" button
mContainerView.addView(rowView, mContainerView.getChildCount() - 1);
}
}
我能做些什么来防止它重叠并出现在下面?每次“更新”时,我如何才能移动按钮?因为如果我将按钮移动到 new_class xml 文件,那么它最初不会显示给我添加更多视图。
我尝试实现以下代码,它的作用是让我第一次正确添加动态视图,但每次都与添加的视图重叠。
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.BELOW, R.id.register_assign_edit);
rowView.setLayoutParams(p);