Programing

Android의 둥근 버튼

lottogame 2020. 11. 3. 07:35
반응형

Android의 둥근 버튼


Android 프로그램에서 둥근 버튼을 만들고 싶습니다. 모서리가 둥근 EditText를 만드는 방법을 살펴 보았습니다 .

내가 달성하고 싶은 것은 :

  1. 둥근 모서리 버튼
  2. 다른 상태에서 버튼 배경 / 모양 변경 (예 : Onclick, Focus)
  3. 내 자신의 PNG를 배경으로 사용하고 모양을 만들지 마십시오.

ImageView에 의존하지 않고 둥근 모서리 버튼을 사용할 수 있습니다.

백그라운드 선택기 리소스 button_background.xml:

<?xml version="1.0" encoding="utf-8" ?> 
     <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <!--  Non focused states 
      --> 
      <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/button_unfocused" /> 
      <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/button_unfocused" /> 
     <!--  Focused states 
      --> 
      <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/button_focus" /> 
      <item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/button_focus" /> 
     <!--  Pressed 
      --> 
      <item android:state_pressed="true" android:drawable="@drawable/button_press" /> 
    </selector>

각 상태에 대해 드로어 블 리소스 (예 : button_press.xml) :

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
  <stroke android:width="1dp" android:color="#FF404040" /> 
  <corners android:radius="6dp" /> 
  <gradient android:startColor="#FF6800" android:centerColor="#FF8000" android:endColor="#FF9700" android:angle="90" /> 
</shape>

corners속성에 유의하십시오 . 이것은 모서리를 둥글게 만듭니다!

그런 다음 버튼에서 배경 드로어 블을 설정합니다.

android:background="@drawable/button_background"

수정 (2018 년 9 월) : 동일한 기술을 사용하여 원형 버튼을 만들 수 있습니다. 원은 실제로 반경 크기가 사각형 측면의 1/2로 설정된 사각형 버튼입니다.

또한 위의 예에서 strokegradient필수 요소는 아니며 둥근 모서리 모양을 볼 수있는 예와 방법 일뿐입니다.


Android에서 둥근 버튼이 필요한 경우 드로어 블로 XML 파일 "RoundShapeBtn.xml"을 만듭니다.

 <?xml version="1.0" encoding="utf-8"?>
  <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" android:padding="10dp">   
  <solid android:color="#6E6E6E"/> <!-- this one is ths color of the Rounded Button -->
  <corners
   android:bottomRightRadius="10dp"
   android:bottomLeftRadius="10dp"
   android:topLeftRadius="10dp"
   android:topRightRadius="10dp"/>
</shape>

버튼 코드에 다음을 추가하십시오.

android:background="@drawable/RoundShapeBtn"

다음과 같이 android의 drawable 폴더에 xml 파일을 만듭니다.

rounded_button.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">

    <corners android:radius="20dp"/> // if you want clear round shape then make radius size is half of your  button`s height.
    <solid android:color="#EEFFFFFF"/> // Button Colour
    <padding
        android:bottom="5dp"
        android:left="10dp"
        android:right="10dp"
        android:top="5dp"/>

</shape>

이제이 xml 파일을 버튼 배경으로 사용합니다.

 <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/rounded_button"
        android:text="@string/button_text"
        android:textColor="@color/black"/>

다른 플랫폼의 UI 요소를 모방하지 않는 것이 좋습니다 . Android 애플리케이션에 둥근 iOS 스타일 버튼을 넣지 않을 것입니다.


다음 ImageView과 같이 확장하십시오 .

public class RoundedImageView extends ImageView {
  private static final String TAG = "RoundedImageView";

  private float mRadius = 0f;

  public RoundedImageView(Context context) {
    super(context);
  }

  public RoundedImageView(Context context, AttributeSet attrs) {
    super(context, attrs);

    // retrieve styles attributes
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundedView);
    mRadius = a.getDimension(R.styleable.RoundedView_radius, 0f);
    a.recycle();
  }

  @Override
  protected void onDraw(Canvas canvas) {
    // only do this if we actually have a radius
    if(mRadius > 0) {
      RectF rect = new RectF(0, 0, getWidth(), getHeight());
      Path clipPath = new Path();
      clipPath.addRoundRect(rect, mRadius, mRadius, Path.Direction.CW);
      canvas.clipPath(clipPath);
    }
    super.onDraw(canvas);
  }
}

일반 배경 리소스를 여기에 적용하면 둥근 모서리로 잘 려야합니다.


이것은 corner 속성을 사용하여 수행 할 수 있습니다. 아래 xml을보십시오.

<item>
    <shape android:shape="rectangle" >
        <stroke
            android:height="1.0dip"
            android:width="1.0dip"
            android:color="#ffee82ee" />

        <solid android:color="#ffee82ee" />

        <corners
            android:bottomLeftRadius="102.0dip"
            android:bottomRightRadius="102.0dip"
            android:radius="102.0dip"
            android:topLeftRadius="102.0dip"
            android:topRightRadius="102.0dip" />
    </shape>
</item>


It's much better to put the button states and shapes in 1 xml selector file. That should make your app run faster/better. Try this (courtesy of Introduction to Android Application Development). Not spamming here just showing this isn't my code.

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:state_pressed="true" >
    <shape android:shape="rectangle"  >
     <corners android:radius="12dip" />
     <stroke android:width="1dip" android:color="#333333" />
     <gradient android:angle="-90" android:startColor="#333333"      android:endColor="#555555"  />            
    </shape>
    </item>
    <item android:state_focused="true">
    <shape android:shape="rectangle"  >
     <corners android:radius="12dip" />
     <stroke android:width="1dip" android:color="#333333" />
     <solid android:color="#58857e"/>       
    </shape>
    </item>  
    <item >
    <shape android:shape="rectangle"  >
     <corners android:radius="12dip" />
     <stroke android:width="1dip" android:color="#333333" />
     <gradient android:angle="-90" android:startColor="#333333" android:endColor="#555555" />            
    </shape>
    </item>

    </selector>

P.S.-design tip: gradients and rounded rectangles are best used when you can hardly tell they are there-use wisely.


Rounded buttons can be creating using ring shape drawable also, see http://www.zoftino.com/android-shape-drawable-examples

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:innerRadius="0dp"
    android:shape="ring"
    android:thickness="40dp"
    android:useLevel="false">
    <solid android:color="@color/colorPrimary" />
    <padding android:bottom="50dp"
        android:left="16dp"
        android:right="16dp"
        android:top="50dp"/>
</shape>

Create Drawable resource named btnOval-->then past code ;

 <?xml version="1.0" encoding="utf-8"?>
      <shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="oval"  
android:padding="10dp">   
      <solid android:color="#6E6E6E"/> 
    </shape>

and user inside button tag like,

<Button
andorid:width=""
android:hieght=""
android:background="@Drawable/btnOval"
/>

Try below code Create a drawable file called circular_button.xml and insert the below

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#008577" />
<corners android:bottomRightRadius="100dp"
    android:bottomLeftRadius="100dp"
    android:topRightRadius="100dp"
    android:topLeftRadius="100dp"/>
</shape>

Then change the background of the button to this drawable file

 <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/circle_button"
                android:text="Button"/>

If you want a full circle button you can use the below drawable

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

    <solid
        android:color="#008577"/>

    <size
        android:width="120dp"
        android:height="120dp"/>
</shape>

참고URL : https://stackoverflow.com/questions/9334618/rounded-button-in-android

반응형