有關佈局的問題。如何以編程方式管理線性子佈局?
我想動態地添加此視圖。
邏輯代碼:
linear1 = (LinearLayout) findViewById(R.id.parent);
linear2 = (LinearLayout) findViewById(R.id.chiled);
int len = 4;
for (int i = 1; i <= len; i++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params1.gravity = Gravity.RIGHT;
final int id_txt;
ImageView iv = new ImageView(this);
iv.setId(i);
id_txt = iv.getId();
iv.setBackgroundResource(R.drawable.ic_launcher);
linear1.addView(iv, params);
iv = ((ImageView) findViewById(id_txt));
for (int j = 1; j < 2; j++) {
final int id_;
Button btn = new Button(this);
btn.setId(i);
id_ = btn.getId();
btn.setText("button " + id_);
linear2.addView(btn, params1);
btn = ((Button) findViewById(id_));
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(v.getContext(),
"Button clicked index = " + id_,
Toast.LENGTH_SHORT).show();
}
});
}
// btn.setBackgroundColor(Color.rgb(70, 80, 90));
// linear1.addView(txt, params);
// params.addRule(RelativeLayout.RIGHT_OF, txt.getId());
iv.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Toast.makeText(view.getContext(),
"text clicked index = " + id_txt,
Toast.LENGTH_SHORT).show();
}
});
}
}
XML代碼:在子視圖動態
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/chiled"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>
</LinearLayout>
我想在父視圖中添加圖像和兩個按鈕。
我激勵,使這種類型的視圖形式
Android heterogeneous gridview like pinterest?
應該等作爲
電流輸出作爲
我不知道問題在哪裏。
一個奇怪的問題,我現在面臨在我的編輯如果我代碼看到佈局那麼它表明我android:orientation="vertical"
如果我輪廓顯示android:orientation="horizontal"
爲每個佈局看。這怎麼可能 ?
幫我解決out.Thanks
不,它不能。還有一些其他的資源可用。 –
然後嘗試添加具有特定索引的孩子,如'getChildCount() - 1'。 – Greeny