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

Android 如何以编程方式使小按钮包装小文本,android,android-layout,button,android-button,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.setCli

我想做小纽扣

到目前为止,我已经使文本变小,但文本周围仍然有很多空间。我尝试了以下代码:

    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" />

当我以编程方式进行设置时,它就像setminhight不工作一样。 我可能做错了什么

这是一张图片:

一个标记为按钮的按钮是由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布局按钮(显示正确),然后填写不同的文本并根据需要重复多次?

在eclipse中写下按钮名称。txtName将为您提供许多选项,并选择您想要的任何选项。或者使用开发者的网站

txtName.setHeight(20);
txtName.setWidth(20);
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);
应使用固定大小(宽度或高度的最小值和最大值相等)

代码:

XML:

以下是一个解决方案:

...
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);
}

如果u r使用包裹内容,则y r使用最大值和最小值height@user123为了让它正常工作,我读到“最小高度”和“最大高度”都有默认值。因此,我试图覆盖它们,以防它们是我的问题的原因。您添加的所有内容都是setHeight(17),这应该由布局参数中的wrap content属性动态完成。但是我试过了,按钮的高度和我上面的图片一样,在第一个代码段。这只是一个线性布局。如果我的屏幕大小改变,这没有帮助,我希望它包装内容。但是,如果我将其设为txtName.setHeight(20)或setHeight(100),则会产生相同的按钮。好像配置被忽略了?为我工作。也可以尝试btn.setWidth(dpToPx(44));调整宽度。谢谢
android:maxHeight="40dp"
android:minHeight="40dp"
android:maxWidth="40dp"
android:minWidth="40dp"
...
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);
}