台灣最大程式設計社群網站
線上人數
740
 
會員總數:246740
討論主題:190153
歡迎您免費加入會員
討論區列表 >> Android APP 開發 >> 改變按鈕的weight
[]  
[我要回覆]
1
回應主題 加入我的關注話題 檢舉此篇討論 將提問者加入個人黑名單
改變按鈕的weight
價值 : 50 QP  點閱數:1888 回應數:2
樓主

X.
門外漢
0 20
365 35
發送站內信

各位板友好!
我想在ScrollView的tableLayout裡面,動態得新增按鈕,可是只要我設定它的weight,按鈕就無法出現,
請各位幫我看看是哪邊出問題,謝謝!
xml檔:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
android:id="@+id/linearLayoutDisplay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"

>

<TextView
android:id="@+id/textViewArrow"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="↑"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="50sp"
android:textStyle="bold" />

<TextView
android:id="@+id/textViewFloor"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="0"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="50sp"
android:textStyle="bold" />

</LinearLayout>

<LinearLayout
android:id="@+id/linearLayoutSignal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@id/linearLayoutDisplay"
android:background="#fffacd"
>

<TextView
android:id="@+id/textViewArrowUP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="↑"
android:textColor="#000000"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
<TextView
android:id="@+id/textViewArrowDN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="↓"
android:textColor="#000000"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textViewRoll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="滾動"
android:textColor="#000000"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
android:id="@+id/textViewSign1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="M1"
android:textColor="#000000"
android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
android:id="@+id/textViewSign2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="M2"
android:textColor="#000000"
android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
android:id="@+id/textViewSign3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="M3"
android:textColor="#000000"
android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
android:id="@+id/textViewSign4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="M4"
android:textColor="#000000"
android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
android:id="@+id/textViewSign5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="M5"
android:textColor="#000000"
android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

<ScrollView
android:id="@+id/scrollViewFloor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/linearLayoutSignal"
android:layout_above="@+id/linearLayoutCallCar"
>

<TableLayout
android:id="@+id/tableLayoutFloor"
android:layout_width="match_parent"
android:layout_height="match_parent" >


</TableLayout>

</ScrollView>

<LinearLayout
android:id="@+id/linearLayoutCallCar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"

android:layout_above="@+id/btPageBack" >

<Spinner
android:id="@+id/spinnerCmd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />

<Spinner
android:id="@+id/spinnerAddr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />

<Spinner
android:id="@+id/spinnerBt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />

<Button
android:id="@+id/buttonCallcar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="8"
android:text="叫車" />

</LinearLayout>

<Button
android:id="@+id/btPageBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="false"
android:text="@string/pageback" />

</RelativeLayout>
-----------------------------------------------------------------
java檔:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.es_test);

addTableRow();
}
void addTableRow(){
TableLayout tl = (TableLayout)findViewById(R.id.tableLayoutFloor);
/* Create a new row to be added. */
TableRow tr = new TableRow(this);
TableRow tr1 = new TableRow(this);
TableRow tr2 = new TableRow(this);
/* Create a Button to be the row-content. */
Button b = new Button(this);
Button b1 = new Button(this);
Button b2 = new Button(this);
Button b3 = new Button(this);
Button b4 = new Button(this);
Button b5 = new Button(this);
Button b6 = new Button(this);
Button b7 = new Button(this);
Button b8 = new Button(this);

LinearLayout.LayoutParams pa = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
pa.weight=1f;
b.setLayoutParams(pa);//這行加了之後,按鈕b就不會顯示了。
b1.setLayoutParams(pa);//這行加了之後,按鈕b1就不會顯示了。
pa.weight=8f;
b2.setLayoutParams(pa);//這行加了之後,按鈕b2就不會顯示了。
b.setText("up");
b1.setText("down");
b2.setText("floor");
/* Add Button to row. */
tr.addView(b);
tr.addView(b1);
tr.addView(b2);
tr1.addView(b3);
tr1.addView(b4);
tr1.addView(b5);
tr2.addView(b6);
tr2.addView(b7);
tr2.addView(b8);
/* Add row to TableLayout. */
tl.addView(tr);
tl.addView(tr1,new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
tl.addView(tr2,new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
}
------------------------------------------------------------------------------
新手上路,請各位板友多多指教! ^^"

搜尋相關Tags的文章: [ weight ] ,
本篇文章發表於2013-01-14 10:29
1樓
最有價值解答

香帥
檢舉此回應
請參考
http://www.cnblogs.com/angeldevil/archive/2012/04/08/2437747.html

http://androidbiancheng.blogspot.tw/2011/07/java-layoutwidth-50.html
去找出您的weight適當值去修改,看看有沒像這篇weight讓他暈了
http://qianxuechao.blog.163.com/blog/static/91813499201172353244517/
本篇文章回覆於2013-01-14 22:38
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔--
2樓
作者回應

X.
檢舉此回應
謝謝香帥大大的回應!

後來我把
LinearLayout.LayoutParams pa = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
改成
TableRow.LayoutParams pa = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,1.0f);

以及

tl.addView(tr1,new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
改成
tl.addView(tr,new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));

就可以了~
本篇文章回覆於2013-01-24 13:37
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔--
   
1

回覆
如要回應,請先登入.