How to programmatically make a small button wrap small text

社会主义新天地 提交于 2019-12-10 16:50:33

问题


I want to make small buttons,

So far I have made the text small but there is still alot of space around the text. I have tried the code below:

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

But i want it to appear the same as this layout xml file:

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

It is like the setMinHieght is not working when I do it programmatically. What could i be doing wrong?

Here is an image:

The one labeled buttons was created by the xml and all the other ones programmatically.

here is the layout code for the entire page:

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

EDIT: Possible Solution:

Is there not a way, that i can programmatically get the xml layout button (which displays correctly), and then fill in different text and duplicate it as many times as I need?


回答1:


in eclipse write your button name. txtName then many options will come for you and choose whatever you want. or use developer's website.

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



回答2:


Here is a solution:

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



回答3:


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



回答4:


You should use fixed size (equal min and max values for width or height)

Code:

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"


来源:https://stackoverflow.com/questions/18775470/how-to-programmatically-make-a-small-button-wrap-small-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!