开发者

Android: Alignment text in button using setGravity()

开发者 https://www.devze.com 2023-03-30 00:22 出处:网络
I wolud like to set button\'s gravity value from default center to right using TextView.setGravity() method, but when I call it nothing happens. In LogCat output using getGravity() method I still see

I wolud like to set button's gravity value from default center to right using TextView.setGravity() method, but when I call it nothing happens. In LogCat output using getGravity() method I still see the first value - 17 (center).

Tell me please, how can I change button's gravity value, using setGravity() method?

Here is code snippet which describes setting the view's gravity:

public void setGravity(String gravitation) {
        if(controlledView != null) {
            String gravityValues[] = gravitation.split("|");
        开发者_如何学Python    for(String gravity : gravityValues) {
                if(gravity.equals("center")) {
                    controlledView.setGravity(Gravity.CENTER);
                }
                else if(gravity.equals("top")) {
                    controlledView.setGravity(Gravity.TOP);
                }
                else if(gravity.equals("bottom")) {
                    controlledView.setGravity(Gravity.BOTTOM);
                }
                else if(gravity.equals("right")) {
                    controlledView.setGravity(Gravity.RIGHT);
                }
                else if(gravity.equals("left")) {
                    controlledView.setGravity(Gravity.LEFT);
                }
            }
        }
}

where controlledView is instance of Button class.


TextView textView = (TextView)findViewById(R.id.mytextviewid);
textView.setGravity(Gravity.RIGHT);

I'm assuming you weren't using Gravity.RIGHT; other valid values can be found in the Gravity constants.


I'm sory for this post, found my mistake myself. I pass to split() method Java Regex API's metacharacter '|' and get all symbols of string. Should choose any other split symbols for string.


You can use in XML layout's like > (RightClick->Properties->Gravity->"your option")

0

精彩评论

暂无评论...
验证码 换一张
取 消