如何以编程方式使一个小按钮包装小文本

时间:2013-09-12 22:43:19

标签: android android-layout button android-button

我想制作小按钮,

到目前为止,我已将文字缩小,但文本周围仍有很多空格。我尝试过以下代码:

    LinearLayout layout = (LinearLayout) view.findViewById(R.id.fragment_dds_tag_linearLayout);
    Button txtName = new Button(getActivity(), null, android.R.attr.buttonStyleSmall);
        txtName.setClickable(false);
        txtName.setTextSize(5);
        txtName.setMaxLines(1);
        txtName.setMaxHeight(40);
        txtName.setMinHeight(0);
        txtName.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        txtName.setText(tagsList.get(i));
        layout.addView(txtName);

但我希望它与此布局xml文件看起来相同:

     <Button
    android:id="@+id/button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:minHeight="5dp"
    android:textSize="10sp"
    android:text="Button" />

当我以编程方式执行时,就像setMinHieght无效。  我能做错什么?

这是一张图片:

Buttons

标记的按钮是由xml和所有其他按钮以编程方式创建的。

这是整个页面的布局代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
    android:id="@+id/fragment_dds_rating_ratingBar_textView_heading"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Tags"
    android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
    android:id="@+id/button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:minHeight="5dp"
    android:textSize="10sp"
    android:text="Button" />
<LinearLayout
    android:id="@+id/fragment_dds_tag_linearLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:orientation="horizontal" >
</LinearLayout>

修改 可能的解决方案:

有没有办法,我可以编程方式获取xml布局按钮(显示正确),然后填写不同的文本并根据需要多次复制它?

4 个答案:

答案 0 :(得分:3)

在eclipse中

写下您的按钮名称。 txtName然后会有很多选项供您选择并选择您想要的任何选项。或使用开发者的网站。

Button txtName = new Button(getActivity(), null, android.R.attr.buttonStyleSmall);
    txtName.setClickable(false);
    txtName.setHeight(17);
    txtName.setWidth(25);
    txtName.setTextSize(10);
    txtName.setMaxLines(1);
    txtName.setMaxHeight(5);
    txtName.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

    txtName.setText(tagsList.get(i));
    layout.addView(txtName);

答案 1 :(得分:1)

这是一个解决方案:

...
Button b = new Button(getContext());
b.setText("Text");
b.setPadding(dpToPx(5), 0, dpToPx(5), 0);
// crazy I have to set both of these?...
b.setMinWidth(0);
b.setMinimumWidth(10);
b.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
LinearLayout.LayoutParams params = new 
LinearLayout.LayoutParams(LinearLayout
                .LayoutParams.WRAP_CONTENT, dpToPx(20));
b.setLayoutParams(params);
...

public static int dpToPx(int dp) {
    return (int) (dp * Resources.getSystem().getDisplayMetrics().density);
}

答案 2 :(得分:0)

txtName.setHeight(20);
txtName.setWidth(20);

答案 3 :(得分:0)

您应该使用固定大小(宽度或高度的最小和最大值相等)

代码:

btn.setMaxHeight(40);
btn.setMinHeight(40);
btn.setMaxWidth(40);
btn.setMinimumWidth(40);

XML:

android:maxHeight="40dp"
android:minHeight="40dp"
android:maxWidth="40dp"
android:minWidth="40dp"