为啥看起来我的 androidbutton 的宽度被压缩了?

Posted

技术标签:

【中文标题】为啥看起来我的 androidbutton 的宽度被压缩了?【英文标题】:Why does it look like my androidbutton is compressed in width?为什么看起来我的 androidbutton 的宽度被压缩了? 【发布时间】: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_
android:layout_ 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_
    android:layout_
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:id="@+id/scrollView">

    <LinearLayout
        android:layout_
        android:layout_
        android:orientation="vertical"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/linLayout">

    </LinearLayout>
</ScrollView>

你有什么建议吗?

【问题讨论】:

里面有文字吗?如果您的按钮上有任何文本,那么这些将被视为“内容”,因此它可能会压缩有关文本的图像。 可以贴出你的输出截图吗? 啊啊对,文字是问题所在。我应该使用 ImageButton...谢谢! 【参考方案1】:

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

【讨论】:

【参考方案2】:

让我提出一些建议。

这样你的代码效率更高:

    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,则缩水很正常。尝试在按钮上添加一些文本。

【讨论】:

对不起,我不能投票。它说我需要更多的“声誉”。 接受答案,你应该可以做到。谢谢【参考方案3】:

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

【讨论】:

【参考方案4】:

以防万一如果您想继续使用按钮,请更改

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

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

其中(100, 100) 是您想要的按钮widthheight

【讨论】:

以上是关于为啥看起来我的 androidbutton 的宽度被压缩了?的主要内容,如果未能解决你的问题,请参考以下文章

android button为啥会用阴影

为啥表格单元格宽度不随 CSS 改变?

为啥屏幕宽度低于 300px 时图像尺寸不减小?

iPhone App - 为啥我的 CGContext 圆圈看起来像黑色方块?

为啥弹性项目宽度随着 flex-grow 增长:0 [重复]

为啥页脚不是从左到右一直走?