2016-12-29 2 views
0

Ich bin programmgesteuert eine Button in einem TableRow hinzufügen. Wenn ich die Schaltfläche hinzufüge, erscheint die Schaltfläche an der Stelle, wo ich möchte, dass es so aussieht, als ob die Schaltfläche nach unten verschoben ist. Ich möchte, dass der Button mittig ausgerichtet ist. Hier ist, wie ich die Taste, um die Tabellenzeile am Hinzufügen, die xml mit meiner Tabellenzeile, und ein Bild von dem, was, was meine Taste aussieht, wenn ich betreibe meine AnwendungSchaltfläche nicht korrekt in 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> 

Antwort

0

Versuchen Sie diesen Code und ich entwarf basierend auf Gewicht-Konzept und ändern Sie es nach Ihrem Wunsch

<?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> 
Verwandte Themen