I have a button that has the following properties:
我有一个按钮具有以下属性:
circle_normal.xml (in res/drawable)
circle_normal。在res /可拉的xml()
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="oval" >
<solid android:color="#FF6347" />
<size
android:height="325dp"
android:width="325dp" />
</shape>
circle.xml (in res/drawable)
圆。在res /可拉的xml()
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/circle_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/circle_normal"></item>
</selector>
activity_main.xml (in res/layout)
activity_main。在res / xml(布局)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Main" >
<Button
android:id="@+id/button_study"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/circle"
android:gravity="center" />
</RelativeLayout>
Main.java (in src)
主要。java(src)
buttonStudy = (Button) findViewById(R.id.button_study);
The final product is that I get a button that is in the shape of a circle. However, due to the difference screen sizes on different Android devices, this one circle size is insufficient. I've looked at a few other questions similar to this but their solutions didn't help me very much. How do I change its size dynamically in the Java code?
最终的结果是,我得到一个圆形的按钮。然而,由于不同的Android设备的屏幕尺寸不同,这个圆的尺寸是不够的。我也看过一些类似的问题,但是他们的解决方案对我帮助不大。如何在Java代码中动态更改它的大小?
2 个解决方案
#1
4
try this way
试试这个方法
final Button buttonStudy = (Button) findViewById(R.id.button_study);
buttonStudy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ViewGroup.LayoutParams params = buttonStudy.getLayoutParams();
params.width = 100;//change the width size
params.height= 100;//change the hight size
buttonStudy.setLayoutParams(params);
}
});
cheerz
cheerz
#2
2
btn = (Button) findViewByID(R.id.button_study);
btn.getLayoutParams().height = x;
btn.getLayoutParams().width = x;
btn.setLayoutParams(btn.getLayoutParams);
Using this code, you can set the height and width of the button. Also, this would avoid the parent view parsing conflict.
使用此代码,您可以设置按钮的高度和宽度。此外,这将避免父视图解析冲突。
Also, you can get the size of screen and then set size of button accordingly:-
另外,你也可以得到屏幕的大小,然后设置相应的按钮大小:-
For API<13
API < 13
Display display = getWindowManager().getDefaultDisplay();
int screenWidth = display.getWidth();
int screenHeight = display.getHeight(); //not required, just to inform
int x = (int) screenWidth/10; //replace 10 by scaling factor
btn = (Button) findViewByID(R.id.button_study);
btn.getLayoutParams().height = x;
btn.getLayoutParams().width = x;
btn.setLayoutParams(btn.getLayoutParams);
For API>=13
对API > = 13
Point size = new Point();
display.getSize(size);
int screenWidth = size.x;
int screenHeight = size.y; //not required, just for info
int x = (int) screenWidth/10; //replace 10 by scaling factor
btn = (Button) findViewByID(R.id.button_study);
btn.getLayoutParams().height = x;
btn.getLayoutParams().width = x;
btn.setLayoutParams(btn.getLayoutParams);
to check for API level at runtime, use
要在运行时检查API级别,请使用
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
//<code> if API>=13 (HONEYCOMB_MR2 is API 13)
}
else {
//<code> if API<13
}
Hope it helps. Happy Programming :)
希望它可以帮助。编程:快乐)
#1
4
try this way
试试这个方法
final Button buttonStudy = (Button) findViewById(R.id.button_study);
buttonStudy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ViewGroup.LayoutParams params = buttonStudy.getLayoutParams();
params.width = 100;//change the width size
params.height= 100;//change the hight size
buttonStudy.setLayoutParams(params);
}
});
cheerz
cheerz
#2
2
btn = (Button) findViewByID(R.id.button_study);
btn.getLayoutParams().height = x;
btn.getLayoutParams().width = x;
btn.setLayoutParams(btn.getLayoutParams);
Using this code, you can set the height and width of the button. Also, this would avoid the parent view parsing conflict.
使用此代码,您可以设置按钮的高度和宽度。此外,这将避免父视图解析冲突。
Also, you can get the size of screen and then set size of button accordingly:-
另外,你也可以得到屏幕的大小,然后设置相应的按钮大小:-
For API<13
API < 13
Display display = getWindowManager().getDefaultDisplay();
int screenWidth = display.getWidth();
int screenHeight = display.getHeight(); //not required, just to inform
int x = (int) screenWidth/10; //replace 10 by scaling factor
btn = (Button) findViewByID(R.id.button_study);
btn.getLayoutParams().height = x;
btn.getLayoutParams().width = x;
btn.setLayoutParams(btn.getLayoutParams);
For API>=13
对API > = 13
Point size = new Point();
display.getSize(size);
int screenWidth = size.x;
int screenHeight = size.y; //not required, just for info
int x = (int) screenWidth/10; //replace 10 by scaling factor
btn = (Button) findViewByID(R.id.button_study);
btn.getLayoutParams().height = x;
btn.getLayoutParams().width = x;
btn.setLayoutParams(btn.getLayoutParams);
to check for API level at runtime, use
要在运行时检查API级别,请使用
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
//<code> if API>=13 (HONEYCOMB_MR2 is API 13)
}
else {
//<code> if API<13
}
Hope it helps. Happy Programming :)
希望它可以帮助。编程:快乐)