获得编程方式创建按钮,从9Patch正确实现填充 [英] Get programmatically created button to correctly implement padding from 9Patch

查看:245
本文介绍了获得编程方式创建按钮,从9Patch正确实现填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下图所示为背景的按钮9Patch绘制。结果
当按钮被吸引它们不会出现被使用作为默认在9Patch定义的填充。

我已经搜查,发现其他人有同样的问题。有人建议,这个问题是默认的按钮样式将覆盖在9Patch定义的填充。一个用户通过设置的Andr​​oid解决问题:填充=@空在他们的XML。

我然而,需要实现XML中的相同,而不是编程的。结果
在我的测试应用程序下面我曾试过无数的东西拿到按钮,以反映在9Patch定义的填充:


  1. 设置所有边距设置为0的的LayoutParams

  2. 所有填充的按钮,手动设置为0(参考按钮A)

  3. 按钮的最低高度设定查看以比文本的高度小的值。 (参考键B)

  4. 设置的最小高度按钮的的TextView 以比文本的高度小的值。 (参考键C)

  5. 设置两个按钮的最低高度查看&安培;按钮的的TextView 以比文本的高度小的值(换句话说,结合测试3和4)。 (参考按钮D)

  6. 手动填充从9Patch转移按dandc87的建议。 (参考键E)

  7. 使用9Patch与ImageButton的,而不是一个按钮。 (参考键F)

正如你可以看到下面,唯一的按钮D(测试5)的ImageButton F(试验7)实施作为9Patch定义的填充。为什么会这样?

在View和TextView的最小高度设置为一个很小的值似乎是一个混乱的黑客。结果
我的问题的核心是: 什么是得到一个编程方式创建按钮,以正确反映在9Patch定义填充的正确方法结果
结果
下面,我在层次视图检查每个测试按钮后,列了一些信息的。

从层次结构视图

  //测量信息
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//按钮A B C D E F
// ------------------------------------------
// mMeasuredHeight 64 64 64 44 64 54
// mMeasuredWidth 85 85 85 85 85 57
// mMinHeight 64 1 64 1 64 0
// mMinWidth 85 85 85 85 85 0
//从层次视图填充信息
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//按钮A B C D E F
// ------------------------------------------------ ------------------------------------------
// mPaddingBottom 4 4 4 4 4 4
// mPaddingLeft 7 7 7 7 7 7
// mPaddingRight 7 7 7 7 7 7
// mPaddingTop 7 7 7 7 7 7
// mUserPaddingBottom 4 4 4 4 4 4
// mUserPaddingEnd 0 -2147483648 -2147483648 -2147483648 -2147483648 -2147483648
// mUserPaddingLeft 7 7 7 7 7 7
// mUserPaddingRight 7 7 7 7 7 7
// mUserPaddingStart 0 -2147483648 -2147483648 -2147483648 -2147483648 -2147483648

注意:结果
在的测试2 的我已经打过电话 setPaddingRelative()其中,Android的参考文档指出相当于XML属性的android:填充(我试过填充设置为0,也为-1)。

 公共类MainActivity延伸活动{    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);        RelativeLayout的mainLayout =(RelativeLayout的)findViewById(R.id.main_layout);
        的LinearLayout testLayout =新的LinearLayout(本);
        mainLayout.addView(testLayout);        //测试(1):设置所有边距设置为0的的LayoutParams
        //结果:无影响
        LinearLayout.LayoutParams PARAMS =新LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
        params.setMargins(0,0,0,0);        //测试(2):所有的填充设置为0 btnA
        //结果:无影响
        按钮btnA =新按钮(本);
        btnA.setText(BTN A);
        btnA.setPaddingRelative(0,0,0,0);
        btnA.setBackground(getResources()getDrawable(R.drawable.test_background));
        testLayout.addView(btnA,则params);        //测试(3):设置视图btnB的最小高度为一个值小于文本的高度
        //结果:无影响
        按钮btnB =新按钮(本);
        btnB.setText(BTN B);
        btnB.setMinimumHeight(1);
        btnB.setBackground(getResources()getDrawable(R.drawable.test_background));
        testLayout.addView(btnB,则params);        //测试(4):TextView的为btnC的最低高度设定为比文字高度小的值
        //结果:无影响
        按钮btnC =新按钮(本);
        btnC.setText(BTN C);
        btnC.setMinHeight(1);
        btnC.setBackground(getResources()getDrawable(R.drawable.test_background));
        testLayout.addView(btnC,则params);        //测试(5):既设置查看&放大器的最小高度; TextView的为btnD
        //比文字高度小的值
        //结果:作为9Patch定义的按钮显示来实现填充
        按钮btnD =新按钮(本);
        btnD.setText(BTN D);
        btnD.setMinimumHeight(1);
        btnD.setMinHeight(1);
        btnD.setBackground(getResources()getDrawable(R.drawable.test_background));
        testLayout.addView(btnD,则params);        //测试(6):手动设置从9Patch为btnE填充按dandc87的建议。
        //结果:无影响
        按钮btnE =新按钮(本);
        btnE.setText(BTN E);
        btnE.setBackground(getResources()getDrawable(R.drawable.test_background));
        NinePatchDrawable IMG =(NinePatchDrawable)getResources()getDrawable(R.drawable.test_background)。
        矩形填充=新的矩形();
        img.getPadding(填充);
        btnE.setPadding(padding.left,padding.top,padding.right,padding.bottom);
        testLayout.addView(btnE,则params);        //测试(7):使用9Patch用的ImageButton代替一个按钮
        //结果:作为9Patch定义的按钮显示来实现填充
        的ImageButton btnF =新的ImageButton(本);
        btnF.setImageResource(android.R.drawable.ic_menu_add);
        btnF.setBackground(getResources()getDrawable(R.drawable.test_background));
        testLayout.addView(btnF,则params);    }
}


解决方案

要获得从9patch填充,你可以尝试:

  NinePatchDrawable IMG =(NinePatchDrawable)getResources()getDrawable(R.drawable.my_9p)。
矩形填充=新的矩形();
img.getPadding(填充);
//填充现在包含填充

您就可以使用填充设置为 setPaddingRelative()

按钮的填充

I am using the 9Patch drawable shown below as the background for buttons.
When the buttons are drawn they do not appear to be using the padding as defined in the 9Patch by default.

I have searched and found others have had the same problem. It is suggested that the issue is that the default button style overrides the padding defined in the 9Patch. One user solved the issue by setting android:padding="@null" in their XML.

I however, need to achieve the same programmatically instead of in XML.
In my test app below I have tried numerous things to get the buttons to reflect the padding as defined in the 9Patch:

  1. Setting all margins to 0 for the LayoutParams
  2. Manually setting all padding for the button to 0. (Ref. Button A)
  3. Setting the minimum height of the button View to a value smaller than the height of the text. (Ref. Button B)
  4. Setting the minimum height of the button's TextView to a value smaller than the height of the text. (Ref. Button C)
  5. Setting both the minimum height of the button View & the button's TextView to a value smaller than the height of the text (in other words, combining tests 3 & 4). (Ref. Button D)
  6. Manually transferring the padding from the 9Patch as per dandc87's suggestion. (Ref. Button E)
  7. Using the 9Patch with an ImageButton instead of a Button. (Ref. Button F)

As you can see below, only Button D (Test 5) and ImageButton F (Test 7) implement the padding as defined in the 9Patch. Why is this so?

Setting both the View and TextView minimum height to a small value seems like a messy hack.
The core of my question is: What is the correct way to get a programmatically created button to correctly reflect the padding defined in a 9Patch.

Below I've tabulated some of the info after inspecting each test button in Hierarchy View.

// Measurement info from Hierarchy View
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Button            A   B   C   D   E   F
// ------------------------------------------
// mMeasuredHeight   64  64  64  44  64  54
// mMeasuredWidth    85  85  85  85  85  57
// mMinHeight        64  1   64  1   64  0
// mMinWidth         85  85  85  85  85  0


// Padding info from Hierarchy View 
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Button                A  B            C            D            E            F
// ------------------------------------------------------------------------------------------
// mPaddingBottom        4  4            4            4            4            4
// mPaddingLeft          7  7            7            7            7            7
// mPaddingRight         7  7            7            7            7            7
// mPaddingTop           7  7            7            7            7            7
// mUserPaddingBottom    4  4            4            4            4            4
// mUserPaddingEnd       0  -2147483648  -2147483648  -2147483648  -2147483648  -2147483648
// mUserPaddingLeft      7  7            7            7            7            7
// mUserPaddingRight     7  7            7            7            7            7
// mUserPaddingStart     0  -2147483648  -2147483648  -2147483648  -2147483648  -2147483648

NOTE:
In Test 2 I have tried calling setPaddingRelative() which the android Reference documentation indicates is equivalent to the XML attribute android:padding (I tried setting the padding to 0 and also to -1).

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RelativeLayout mainLayout = (RelativeLayout)findViewById(R.id.main_layout);
        LinearLayout testLayout = new LinearLayout(this);
        mainLayout.addView(testLayout);

        // Test (1): Setting all margins to 0 for the LayoutParams
        //   Result: No Effect      
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 0, 0, 0);

        // Test (2): Setting all padding to 0 for btnA
        //   Result: No Effect
        Button btnA = new Button(this);
        btnA.setText("Btn A");
        btnA.setPaddingRelative(0, 0, 0, 0);
        btnA.setBackground(getResources().getDrawable(R.drawable.test_background));
        testLayout.addView(btnA, params);

        // Test (3): Setting minimum height of the View btnB to a value smaller than the height of the text
        //   Result: No Effect
        Button btnB = new Button(this);
        btnB.setText("Btn B");
        btnB.setMinimumHeight(1);
        btnB.setBackground(getResources().getDrawable(R.drawable.test_background));
        testLayout.addView(btnB, params);

        // Test (4): Setting minimum height of the TextView for btnC to a value smaller than the height of the text
        //   Result: No Effect
        Button btnC = new Button(this);
        btnC.setText("Btn C");
        btnC.setMinHeight(1);
        btnC.setBackground(getResources().getDrawable(R.drawable.test_background));
        testLayout.addView(btnC, params);

        // Test (5): Setting minimum height of both the View & the TextView for btnD 
        //           to a value smaller than the height of the text
        //   Result: Button appears to implement padding as defined in the 9Patch
        Button btnD = new Button(this);
        btnD.setText("Btn D");
        btnD.setMinimumHeight(1); 
        btnD.setMinHeight(1);
        btnD.setBackground(getResources().getDrawable(R.drawable.test_background));
        testLayout.addView(btnD, params);

        // Test (6): Manually setting the padding for btnE from the 9Patch as per dandc87's suggestion.
        //   Result: No Effect
        Button btnE = new Button(this);
        btnE.setText("Btn E");
        btnE.setBackground(getResources().getDrawable(R.drawable.test_background));
        NinePatchDrawable img = (NinePatchDrawable) getResources().getDrawable(R.drawable.test_background);
        Rect padding = new Rect();
        img.getPadding(padding);
        btnE.setPadding(padding.left, padding.top, padding.right, padding.bottom);  
        testLayout.addView(btnE, params);

        // Test (7): Using the 9Patch with an ImageButton instead of a Button
        //   Result: Button appears to implement padding as defined in the 9Patch
        ImageButton btnF = new ImageButton(this);
        btnF.setImageResource(android.R.drawable.ic_menu_add);
        btnF.setBackground(getResources().getDrawable(R.drawable.test_background));
        testLayout.addView(btnF, params);

    }
}

解决方案

To get the padding from the 9patch, you could try:

NinePatchDrawable img = (NinePatchDrawable) getResources().getDrawable(R.drawable.my_9p);
Rect padding = new Rect();
img.getPadding(padding);
//padding now contains the padding

You can then use padding to set the padding of the Button with setPaddingRelative()

这篇关于获得编程方式创建按钮,从9Patch正确实现填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆