android 中的完美圆形按钮
Perfectly Round Buttons in android
我的应用程序中需要完美的圆形按钮。根据屏幕尺寸自动缩放(它所在的布局高度。)
我的 xml 文件:circle1try.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
<corners
android:radius="1000dp"
/>
<solid
android:color="#ff00b3ff"
/>
</shape>
我在 activity 布局中使用它的地方:
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:padding="30dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/button1Key"
android:typeface="monospace"
android:textSize="20dp"
android:background="@drawable/circle1try"
android:textColor="#ffffffff"
android:text="tap here"
android:layout_gravity="center"/>
</LinearLayout>
整个LinearLayout
位于另一个垂直方向LinearLayout
,我使用layout_weight
确保布局占用的space在所有屏幕上的比例相似。
java 文件中与按钮相关的代码片段
roundButton=(Button)findViewById(R.id.button1Key);
int btnSize=roundButton.getLayoutParams().height;
roundButton.setLayoutParams(new LinearLayout.LayoutParams(btnSize, btnSize));
在我的应用程序中,根据用户操作,我还需要更改颜色,我在同一 class.
中使用了以下代码片段
bgShape = (GradientDrawable)roundButton.getBackground();
bgShape.setColor(Color.BLACK);
但是我使用这个过程得到的按钮不是一个完整的圆。
它接近圆形但沿 x 轴较宽。
那么如何获得一个可以轻松更改颜色的完美圆形按钮?
您可以使用这个库:
AndroidCircleButton
或
看看 another answer on Whosebug
好的!因此,代码基本上不起作用的是,它甚至在绘制布局之前就按比例放大了形状。解决方案 ?等待绘制布局。
最初 getHeight()
会 return 0
。
我使用了 ViewTreeObserver。
现在它 return 是实际值。
我合并的代码片段解决了我的问题。
ViewTreeObserver vto = player.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
roundButton.getViewTreeObserver().removeGlobalOnLayoutListener(this);
roundButton.setWidth(player.getHeight());
ViewGroup.LayoutParams params=roundButton.getLayoutParams();
params.width=player.getHeight();
roundButton.setLayoutParams(params);
}
});
我的应用程序中需要完美的圆形按钮。根据屏幕尺寸自动缩放(它所在的布局高度。)
我的 xml 文件:circle1try.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
<corners
android:radius="1000dp"
/>
<solid
android:color="#ff00b3ff"
/>
</shape>
我在 activity 布局中使用它的地方:
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:padding="30dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/button1Key"
android:typeface="monospace"
android:textSize="20dp"
android:background="@drawable/circle1try"
android:textColor="#ffffffff"
android:text="tap here"
android:layout_gravity="center"/>
</LinearLayout>
整个LinearLayout
位于另一个垂直方向LinearLayout
,我使用layout_weight
确保布局占用的space在所有屏幕上的比例相似。
java 文件中与按钮相关的代码片段
roundButton=(Button)findViewById(R.id.button1Key);
int btnSize=roundButton.getLayoutParams().height;
roundButton.setLayoutParams(new LinearLayout.LayoutParams(btnSize, btnSize));
在我的应用程序中,根据用户操作,我还需要更改颜色,我在同一 class.
中使用了以下代码片段bgShape = (GradientDrawable)roundButton.getBackground();
bgShape.setColor(Color.BLACK);
但是我使用这个过程得到的按钮不是一个完整的圆。 它接近圆形但沿 x 轴较宽。 那么如何获得一个可以轻松更改颜色的完美圆形按钮?
您可以使用这个库: AndroidCircleButton
或 看看 another answer on Whosebug
好的!因此,代码基本上不起作用的是,它甚至在绘制布局之前就按比例放大了形状。解决方案 ?等待绘制布局。
最初 getHeight()
会 return 0
。
我使用了 ViewTreeObserver。
现在它 return 是实际值。
我合并的代码片段解决了我的问题。
ViewTreeObserver vto = player.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
roundButton.getViewTreeObserver().removeGlobalOnLayoutListener(this);
roundButton.setWidth(player.getHeight());
ViewGroup.LayoutParams params=roundButton.getLayoutParams();
params.width=player.getHeight();
roundButton.setLayoutParams(params);
}
});