[英]Adding Android buttons to view dynamically
我已經看過以下問題以及其他問題: 如何在Android中動態添加按鈕?
我有2個活動片段。 每次用戶在第二個活動上按下按鈕時,他們將被重定向到主活動,並且按鈕將添加到頁面中。 即使將按鈕正確地保存在MainActivity中的數組中,布局中也僅顯示最新的按鈕,但是將按鈕向下推,就像其他按鈕在其上方一樣。
在MainActivity中:
private static List<Button> ideas = new ArrayList<Button>();
private static SharedPreferences sharedPref;
private Context myContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout layout = new RelativeLayout(this);
setContentView(layout, new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
Intent intent = getIntent();
Button newIdea = new Button(this);
newIdea.setText("NEW IDEA");
newIdea.setTranslationX(300);
newIdea.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), NewIdeaPageActivity.class);
startActivity(intent);
}
});
layout.addView(newIdea);
if (intent != null) {
if (intent.hasExtra("title")) {
Button button = new Button(this);
button.setId(ideas.size());
button.setText(getIntent().getStringExtra("title"));
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
if (ideas.size() == 0) {
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
} else {
params.addRule(RelativeLayout.BELOW, ideas.size() - 1);
}
layout.addView(button, params);
ideas.add(button);
}
}
}
MainActivity xml:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/fragment"
android:name="com.zarwanhashem.ideatrackr.MainActivityFragment"
tools:layout="@layout/fragment_main" android:layout_width="match_parent"
android:layout_height="match_parent" />
MainActivity片段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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivityFragment"
android:id="@+id/fragment_main_layout">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Idea"
android:id="@+id/new_idea_button"
android:onClick="onNewIdeaButtonClicked"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
新的想法按鈕將他們帶到第二個活動,在此他們創建添加到主要活動的按鈕。 知道為什么實際上只顯示最新按鈕嗎? 調試時,所有按鈕都顯示在ideas數組中。
您有一個RelativeLayout作為父級。 這意味着,您必須告訴孩子們應該在哪里,然后用例如
android:layout_alignParentTop
就像在xml中一樣。 (順便說一下,其中一些陣營將自己取消了)
閱讀http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html
您會發現,還有
android:layout_below
需要一個ID。
您要做的就是給您生成的按鈕一個ID,可以是一個數字。 然后,您必須將規則添加到RelativeLayout layoutparams中。
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT)
params.addRule(RelativeLayout.BELOW, IDofPreviousButtonComesInHere);
並刪除這些setTranslations()。
但是,正如所說的,請看一下ListView(或新的RecyclerView)。
如果您看過教程,請不要在http://developer.android.com/guide/topics/ui/layout/listview.html上進行操作 ,否則就沒有好的開始。 試試這個http://windrealm.org/tutorials/android/android-listview.php 。 只需用一個按鈕交換planets數組和您的idea數組以及textview xml。 它應該工作。
編輯:
試試這個,行得通。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout layout = new RelativeLayout(this);
setContentView(layout, new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
for(int i = 0; i<10; i++) {
Button button = new Button(this);
int id = i+1000;
button.setId(id);
button.setText("Button " + i);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
if(i==0) {
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
} else {
params.addRule(RelativeLayout.BELOW, id-1);
}
layout.addView(button, params);
}
}
每次調用onCreate()時,特別是在調用setContentView()時,整個布局都會被清除干凈,並使用R.layout.activity_main加載。
之所以只看到最新的按鈕,是因為您只在onCreate()中最多添加了一個按鈕。 該按鈕的位置是偏移的,因為您是根據想法數組中按鈕的數量來計算其y位置的。
我會做這樣的事情:
if (intent != null) {
if (intent.hasExtra("title")) {
ideas.add(new Button(myContext));
ideas.get(ideas.size() - 1).setLayoutParams(new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT));
ideas.get(ideas.size() - 1).setTranslationY(ideas.size() * 100 + 100);
ideas.get(ideas.size() - 1).setText(intent.getStringExtra("title"));
}
}
// add all the buttons to the layout hierarchy
for (Button b : ideas) {
layout.addView(b);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.