2016-12-29 51 views
0

我以編程方式在TableRow中添加Button。當我添加按鈕時,按鈕顯示在我想要的位置,除了它看起來像按鈕被向下移動。我想讓按鈕居中對齊。這裏是我正在添加的按鈕,錶行,我的錶行的XML,和什麼我的按鈕看起來什麼,當我跑我的應用程序如圖片按鈕在TableRow中沒有正確顯示

enter image description here

public void addNewButtonToRegister(String buttonString, String buttonPrice, Button_Sizes buttonSize){ 
    Button newButton = new Button(getActivity().getApplicationContext()); 
    Button addButton = (Button) mMenuFragment.findViewById(R.id.add_button); 
    int height = addButton.getHeight(); 
    int width = addButton.getWidth(); 
    newButton.setLayoutParams(new TableRow.LayoutParams(height, width)); 
    newButton.setText(buttonString + "\n" + buttonPrice); 
    TableRow firstRow = (TableRow) mMenuFragment.findViewById(R.id.first_row); 
    firstRow.addView(newButton); 
} 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:orientation="vertical" 
      android:padding="@dimen/activity_vertical_margin" 
      android:layout_width="match_parent" 
      android:id="@+id/test" 
      android:layout_height="match_parent"> 

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

    <TableRow 
     android:orientation="horizontal" 
     android:id="@+id/first_row"> 

     <Button 
      android:id="@+id/add_button" 
      android:text="@string/add_button" 
      android:textSize="30dp" 
      android:layout_width="@dimen/register_button_size" 
      android:layout_height="@dimen/register_button_size"/> 



    </TableRow> 

</TableLayout> 

回答

0

試試這個代碼,我根據重量的概念設計,並根據您的意願修改它

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@color/bgcolor" 
    android:orientation="horizontal" 
    android:padding="10dip" > 



    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="25dip" 
     android:gravity="center" 
     android:textSize="24.5sp" 
     android:visibility="gone" /> 

    <TableLayout 
     android:id="@+id/tableLayout1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:stretchColumns="*" > 

     <TableRow 
      android:id="@+id/locationsRowss" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="#ffffff" 
      android:gravity="center_horizontal" 
      android:minHeight="40dp" 
      android:weightSum="2" > 

      <TextView 
       android:id="@+id/tvlocation11" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_marginBottom="0.5dp" 
       android:layout_weight="1" 
       android:background="@drawable/cellshape" 
       android:gravity="left|center" 
       android:paddingLeft="10dp" 
       android:text="Journey Date" 
       android:textColor="@color/textcolor" 
       android:textSize="15dp" 
       android:textStyle="bold" /> 

      <Button 
       android:id="@+id/tvJourneyDate" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_gravity="center" 
       android:layout_marginBottom="0.5dp" 
       android:layout_marginLeft="0.5dp" 
       android:layout_weight="1" 
       android:background="@drawable/cellshape" 
       android:gravity="left|center|center_vertical" 
       android:paddingLeft="10dp" 
       android:paddingRight="10dp" 
       android:text="2/3/2017" 
       android:textColor="@color/vehicledetailcolor" 
       android:textSize="15dp" /> 
     </TableRow> 



</LinearLayout>