Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
267 views
in Technique[技术] by (71.8m points)

android - How to set even width for button view which supports in all mobile devices

I have created Button View programatically inside LinearLayout with VERTICAL orientation. Below is my code for button view

LinearLayout.LayoutParams params= new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        int w=Resources.getSystem().getDisplayMetrics().widthPixels;
        params.setMargins(w/3,0,w/3,0);
        button.setLayoutParams(params);
        button.setText("Hello");
        layout.setGravity(Gravity.CENTER);
layout.addView(button);

But because of the dimensions ButtonView looks different in some low dimension device. For ex. below in low dimension

 _____
|hello|
|_____|

below in high dimension devices,

 _____________
|   hello     |
|_____________|

When I tried wrap content for width and left, right padding for it. Due to the length of the text inside Button, all ButtonView will not look even. For Ex.

 _____________
| Hello World |
|_____________|


 _______
| hello |
|_______|

My goal is to achieve above(mentioned in high dimension) like width for all devices. Thanks in advance.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Maybe you could use wrapContent for width with a paddingHorizontal value?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...