我正在向 ListActivity 动态添加一个按钮。如下:
setContentView(R.layout.listview_singlegrey);
LayoutInflater inflater = (LayoutInflater)this.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout linear = (LinearLayout) findViewById(R.id.list_comment);
Button btAddComment = (Button)inflater.inflate(R.layout.bt_yellow,null);
FrameLayout.LayoutParams lyp1 = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
lyp1.gravity = Gravity.RIGHT;
btAddComment.setLayoutParams(lyp1);
btAddComment.setText("Añadir comentario");
列表视图的 LinearLayout 是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:orientation="vertical"
android:layout_height="fill_parent" android:id="@+id/list_comment"
android:paddingLeft="10dp" android:paddingRight="10dp" android:background="@drawable/fondomain">
<ListView android:id="@+id/android:list" android:listSelector="@android:color/transparent"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:divider="@layout/linedivider" android:dividerHeight="10px"
android:cacheColorHint="#0000" android:paddingTop="10dp"
/>
</LinearLayout>
我正在充气的按钮的布局是:
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/wrapfill"
android:background="@layout/selectoryellowbutton" android:layout_gravity="right" android:padding="10dp"
android:textSize="16px" android:text="Añadir comentario" android:textStyle="bold" android:textColor="@color/darkergray">
</Button>
如您所见,我尝试在代码和按钮中设置 layout_gravity 参数,但它们都不起作用。我仍然在屏幕左侧显示按钮。似乎布局参数不起作用。但是,如果我将 fill_parent 设置为宽度,我会得到整个屏幕上的按钮,并且我希望按钮包裹文本并对齐屏幕右侧。