我想以编程方式在我的活动中添加多个按钮。到目前为止工作正常,但它看起来像我的按钮被压缩的宽度。该按钮具有20 * 20像素的背景图像,并且即时使用WRAP_CONTENT作为宽度和高度。下面是代码:为什么它看起来像我的androidbutton宽度压缩?
for(int i = 0; i < 5; i++){
LinearLayout layout;
Button buttonDel;
layout = (LinearLayout)findViewById(R.id.linLayout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
buttonDel = new Button(this);
buttonDel.setId(i);
buttonDel.setBackgroundResource(R.drawable.buttondelete);
buttonDel.setLayoutParams(params);
layout.addView(buttonDel);
}
和XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.developer.cardz.ListOfAllWordsActivity">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/scrollView">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/linLayout">
</LinearLayout>
</ScrollView>
你有什么建议吗?
它里面有没有文字?如果您的按钮上有任何文字,那么这些文字将被视为“内容”,因此它可能会压缩有关文字的图像。 – Muthu
可以发布你的输出的截图? – droid
aaah对,文字是问题。我应该使用ImageButton ...谢谢! – nic2307