【问题标题】:Android Button text gets cut off: solutions?Android Button 文本被截断:解决方案?
【发布时间】:2013-08-07 10:43:44
【问题描述】:

我有一个问题...我想显示代表字母的按钮列表。比如:

[a] [b] [c] [d] [e] ....

问题在于按钮非常小,并且由于按钮对象的填充而导致文本被截断。有没有办法总是在按钮顶部显示按钮的文本,所以它不会被截断?

我唯一的选择似乎是创建模仿按钮的 JPEG。但这一点也不优雅。

另一个选项是在按钮顶部显示一个 TextView,这可能比之前的选择更优雅一些。

for (...) {
   Button buton = new Button(Activity.this);
   LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1);
   int margin = (int) Math.round(5 * SCALE);
   layoutParams.setMargins(margin, margin, margin, margin);
   buton.setLayoutParams(layoutParams);
   buton.setOnClickListener(new View.OnClickListener() { ... }
   (...)
   linearLayout.add(buton)
}

我该怎么办?

编辑:我以编程方式在 LinearLayout(水平)中创建所有按钮。我不知道按钮的数量,所以这就是我设置权重的原因。而且我的目标 API 至少为 8 (2.2)。

【问题讨论】:

  • 您是否尝试在您的 Button 中设置 android:layout_width="wrap_content"

标签: android android-layout


【解决方案1】:

您可以手动添加填充..您还可以设置按钮的宽度和高度,这样文本就不会消失.. 像这样

    <Button
        android:id="@+id/button1"
        android:layout_width="150dip"
        android:layout_height="150dip"
        android:text="This is a short text" />

或者你可以像这样改变边距

<Button
    android:layout_marginLeft="-3dip"
    android:layout_marginRight="-3dip"
    ... />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-13
    • 2021-12-01
    • 2011-12-04
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多