2015-01-02 70 views
0

我想以编程方式在我的活动中添加多个按钮。到目前为止工作正常,但它看起来像我的按钮被压缩的宽度。该按钮具有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> 

你有什么建议吗?

+0

它里面有没有文字?如果您的按钮上有任何文字,那么这些文字将被视为“内容”,因此它可能会压缩有关文字的图像。 – Muthu

+0

可以发布你的输出的截图? – droid

+0

aaah对,文字是问题。我应该使用ImageButton ...谢谢! – nic2307

回答

0

它里面有没有文字?如果您的按钮上有任何文字,那么这些文字将被视为“内容”,因此它可能会压缩有关文字的图像。

0

让我来做一些建议。

你的代码更有效的是这样的:

LinearLayout layout; 
    Button buttonDel; 

    layout = (LinearLayout)findViewById(R.id.linLayout); 


    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 

    for(int i = 0; i < 5; i++){ 
     buttonDel = new Button(this); 
     buttonDel.setId(i); 
     buttonDel.setBackgroundResource(R.drawable.buttondelete); 
     buttonDel.setLayoutParams(params); 

     layout.addView(buttonDel); 
    } 

而且layout.setOrientation(LinearLayout.VERTICAL);是不需要的,因为你必须在你的XML。

您能否显示R.drawable.buttondelete?如果它是png9,它正常缩小。尝试将一些文字放到按钮上。

+0

嗯谢谢! – nic2307

+0

感谢我与upvote :) 很高兴帮助! – Olaia

+1

对不起,我不能upvote。它说我需要更多的“声誉”。 – nic2307

0

我将Button更改为ImageButton。这解决了问题。感谢所有评论过的人!

0

就在,如果你想继续按钮,改变情况下

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(100, 100);

其中(100, 100)是你想要的按钮宽度高度

相关问题