2012-02-06 69 views
1

基本上我試圖將行添加到一個表,我需要以編程方式做到這一點。的Android RelativeLayout的編程

我可以把它添加我想要的內容,而不是究竟如何我想它。

比如我想在左邊與右邊的按鈕按鍵的EditText一個TextView。

基本上我希望它像下面的圖片:

http://s16.postimage.org/nluqthuut/Untitled_1.png

此外,我不希望只是和寬度TextView的。

反正這裏是我迄今爲止所取得:

XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" android:padding="5dp" xmlns:android="http://schemas.android.com/apk/res/android"> 

    <ScrollView 
     android:id="@+id/scrollView1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" > 

     <LinearLayout 
      android:id="@+id/linearLayout1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" > 

      <Button 
       android:id="@+id/btnOutstandingJob" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="10dp" 
       android:text="Outstanding Job" /> 

      <Button 
       android:id="@+id/btnVanStock" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="10dp" 
       android:text="Van Stock" /> 

      <Button 
       android:id="@+id/btnRegister" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="5dp" 
       android:text="Register User" /> 
     </LinearLayout> 

    </ScrollView> 

    <RelativeLayout 
     android:id="@+id/relativeLayout1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 

     <Button 
      android:id="@+id/btnLogout" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_marginTop="5dp" 
      android:text="Log Out" /> 
    </RelativeLayout> 

</LinearLayout> 

的Java:

TableLayout table = (TableLayout) findViewById(R.id.tableLayout1); 

TableRow row = new TableRow(vanstock.this); 

RelativeLayout RowLayout = new RelativeLayout(vanstock.this); 
RowLayout.setId(99); 

TextView lblItem = new TextView(vanstock.this); 
lblItem.setId(1); 
lblItem.setText("ddfsgsdfgs"); 

RelativeLayout.LayoutParams lblitemParam = new RelativeLayout.LayoutParams(
     RelativeLayout.LayoutParams.WRAP_CONTENT, 
     RelativeLayout.LayoutParams.WRAP_CONTENT); 
lblitemParam.addRule(RelativeLayout.CENTER_VERTICAL, 
     RelativeLayout.TRUE); 
lblitemParam.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 
     RelativeLayout.TRUE); 

lblItem.setLayoutParams(lblitemParam); 
RowLayout.addView(lblItem); 

Button btnPlus = new Button(vanstock.this); 
btnPlus.setId(4); 
btnPlus.setText("+"); 
btnPlus.setWidth(40); 

RelativeLayout.LayoutParams btnPlusParams = new RelativeLayout.LayoutParams(
     RelativeLayout.LayoutParams.WRAP_CONTENT, 
     RelativeLayout.LayoutParams.WRAP_CONTENT); 
btnPlusParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, 
     RelativeLayout.TRUE); 
btnPlusParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 
     RelativeLayout.TRUE); 
btnPlus.setLayoutParams(btnPlusParams); 

RowLayout.addView(btnPlus); 

EditText txtItem = new EditText(vanstock.this); 
txtItem.setId(3); 
txtItem.setWidth(40); 

RelativeLayout.LayoutParams txtItemParams = new RelativeLayout.LayoutParams(
     RelativeLayout.LayoutParams.WRAP_CONTENT, 
     RelativeLayout.LayoutParams.WRAP_CONTENT); 
txtItemParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, 
     RelativeLayout.TRUE); 
txtItemParams.addRule(RelativeLayout.LEFT_OF, btnPlus.getId()); 
txtItem.setLayoutParams(txtItemParams); 

RowLayout.addView(txtItem); 

Button btnMinus = new Button(vanstock.this); 
btnMinus.setId(2); 
btnMinus.setText("-"); 
btnMinus.setWidth(40); 

RelativeLayout.LayoutParams btnMinusParams = new RelativeLayout.LayoutParams(
     RelativeLayout.LayoutParams.WRAP_CONTENT, 
     RelativeLayout.LayoutParams.WRAP_CONTENT); 
btnMinusParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE); 
btnMinusParams.addRule(RelativeLayout.LEFT_OF, txtItem.getId()); 
btnPlus.setLayoutParams(btnMinusParams); 

RowLayout.addView(btnPlus); 
row.addView(RowLayout); 

table.addView(row, new LayoutParams(LayoutParams.WRAP_CONTENT, 
     LayoutParams.WRAP_CONTENT)); 

我試着搜索等,但我現在還不能讓它正常工作..任何幫助,將不勝感激!

+0

我認爲你正在做的太複雜了。如果我想鏈接的圖像中進行的結構類似,我會用從XML文件充氣行一個ListView。通過這種方式,所有的圖形用戶界面的東西都將在xml中定義,並且您在Java中只能做很少的細節。 – 2012-02-06 14:16:09

回答

2
LayoutInflater inflater = getLayoutInflater(); 
TableRow row = (TableRow) inflater.inflate(R.layout.table, 
       _tablelayout, false); 
TextView textview = (TextView) row.findViewById(R.id.rowdata); 

像這樣,你可以滿足你的要求。這就足夠了嗎?

最佳 V.k

+0

我想我明白是的,所以我就與剛剛在錶行一個xml?然後像這樣? TableLayout table =(TableLayout)findViewById(R.id.tableLayout1); \t \t \t \t LayoutInflater inflater = getLayoutInflater(); \t \t \t \t的TableRow行=(的TableRow)inflater.inflate(R.layout.tablerow, \t \t \t \t \t \t表,假); – 2012-02-06 14:18:01

+0

是啊,當然你明白fine.Try這個,否則我會幫你2maro .. – 2012-02-06 14:30:24

+0

一些奇怪的結果後玩耍..然而,玩過後,我想我把它回到它是如何,現在它工作完美? :令人困惑,但謝謝:) – 2012-02-06 14:47:00