【发布时间】:2015-01-02 11:27:52
【问题描述】:
我想以编程方式在我的活动中添加多个按钮。到目前为止效果很好,但看起来我的按钮宽度被压缩了。该按钮有一个 20*20 像素的背景图像,我使用 WRAP_CONTENT 作为宽度和高度。代码如下:
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>
你有什么建议吗?
【问题讨论】:
-
里面有文字吗?如果您的按钮上有任何文本,那么这些将被视为“内容”,因此它可能会压缩有关文本的图像。
-
可以贴出你的输出截图吗?
-
啊啊对,文字是问题所在。我应该使用 ImageButton...谢谢!
标签: android button programmatically-created