【问题标题】: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_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


【解决方案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

      【讨论】:

        猜你喜欢
        • 2012-11-19
        • 2022-12-03
        • 2016-01-08
        • 1970-01-01
        • 1970-01-01
        • 2014-07-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多