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