我有一种情况,我需要在运行时添加布局,如下所示:
1.Button
XML 中已经定义了一个布局:
2.当用户点击 时Button
,anEditText
和 anotherButton
被动态添加到现有布局中:
它在 Gingerbread (Android 2.3.5) 上正常工作:
并且不适用于 Kitkat(Android 4.4.2):
有人知道这个问题的解决方法吗?这是我使用的代码:
public class MainActivity extends Activity {
private boolean comment = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.commentbutton);
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
if(comment == true){
comment = false;
LinearLayout l = (LinearLayout)findViewById(R.id.comment_layout);
l.setOrientation(LinearLayout.VERTICAL);
final EditText e = new EditText(v.getContext());
e.setId(R.id.commentbox);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(250,100);
lp.setMargins(0, 20, 0, 0);
e.setLayoutParams(lp);
e.setSingleLine(true);
e.setImeOptions(EditorInfo.IME_ACTION_DONE);
l.addView(e);
final Button b = new Button(v.getContext());
b.setId(R.id.submitbutton);
lp = new LinearLayout.LayoutParams(150,33);
lp.setMargins(0, 20, 0, 0);
b.setLayoutParams(lp);
b.setText("Submit");
b.setTextColor(Color.BLACK);
b.setTypeface(null, Typeface.BOLD);
b.setBackgroundColor(Color.parseColor("#CCCCCC"));
b.setFocusable(true);
b.setFocusableInTouchMode(true);
l.addView(b);
e.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
b.performClick();
return true;
}
return false;
}
});
LinearLayout ll = new LinearLayout(v.getContext());
ll.setId(R.id.productbottomlayout);
lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,25);
ll.setLayoutParams(lp);
ll.setFocusable(true);
ll.setFocusableInTouchMode(true);
l.addView(ll);
ll.requestFocus();
b.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
LinearLayout l = (LinearLayout) findViewById(R.id.comment_layout);
EditText e = (EditText) findViewById(R.id.commentbox);
l.removeView(e);
Button b = (Button) findViewById(R.id.submitbutton);
l.removeView(b);
LinearLayout ll = (LinearLayout) findViewById(R.id.productbottomlayout);
l.removeView(ll);
MainActivity.this.comment = true;
}
});
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
这是 XML 布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/comment_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="25dp"
android:layout_gravity="center"
android:gravity="center">
<Button
android:id="@+id/commentbutton"
android:layout_width="150dp"
android:layout_height="30dp"
android:text="Add Comment"
android:textColor="#000000"
android:textStyle="bold"
android:background="#CCCCCC"
android:onClick="showCommentBox"/>
</LinearLayout>
</RelativeLayout>
此外,当我们像这样以编程方式添加布局时,我们需要为每个布局元素手动设置一个 ID。
我已经做到了。他们来了:
<resources>
<item type = "id" name = "commentbox"></item>
<item type = "id" name = "submitbutton"></item>
<item type="id" name="productbottomlayout"></item>
</resources>