java将按钮添加到指定位置_在运行时将按钮添加到布局
我正在開發一個Android項目,我需要在運行時向Layout添加按鈕 . 所以,這是布局xml:
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layout">
這是活動:
import android.app.Activity;
import android.widget.*;
import android.widget.TableLayout.LayoutParams;
import android.os.Bundle;
public class PluginsActivity extends Activity{
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.plugins);
LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
Button butt = new Button(this);
Button butt2 = new Button(this);
butt.setText("lol");
butt2.setText("lol2");
butt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
butt2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
layout.addView(butt);
layout.addView(butt2);
}
}
現在,問題是,當我開始活動時,按鈕1“覆蓋”按鈕2.事實上,如果我使用LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT),則顯示兩個按鈕 . 有沒有辦法在LinearLayout上添加“新行”?屏幕應該是這樣的:
| *********** Button1 ************** |
| *********** Button2 ************** |
謝謝 .
總結
以上是生活随笔為你收集整理的java将按钮添加到指定位置_在运行时将按钮添加到布局的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 歇后语外甥打灯笼下一句是什么啊?
- 下一篇: java调用npoi_NPOI导出数据到