我想做一個button
回合的角落。有沒有一種簡單的方法可以在Android中實現這一點?如何使一個按鈕的角落圓?
回答
如果你想是這樣的
這裏是代碼。
1,創建像mybutton.xml您繪製文件夾中的XML文件,並粘貼以下標記:
<?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="3dip" />
<stroke android:width="1dip" android:color="#5e7974" />
<gradient android:angle="-90" android:startColor="#345953" android:endColor="#689a92" />
</shape>
</item>
<item android:state_focused="true">
<shape android:shape="rectangle" >
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#5e7974" />
<solid android:color="#58857e"/>
</shape>
</item>
<item >
<shape android:shape="rectangle" >
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#5e7974" />
<gradient android:angle="-90" android:startColor="#8dbab3" android:endColor="#58857e" />
</shape>
</item>
</selector>
2.Now使用此繪製你的視圖的背景。如果該視圖按鈕,然後是這樣的:
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="#ffffff"
android:background="@drawable/mybutton"
android:text="Buttons" />
它崩潰:原因:org.xmlpull.v1.XmlPullParserException:二進制XML文件行#24:
我們可以通過編程來實現。 – Killer 2015-07-24 15:00:17
它說元素選擇器必須聲明編輯:對不起,該文件是在值文件夾而不是drawable。當我轉移它時,它工作。 – 2016-11-17 07:51:26
如果您使用的是矢量繪圖,那麼您只需在您的可繪製定義中指定<角落>元素。我已經在blog post中報道過這個問題。
如果您使用位圖/ 9-補丁drawables,則需要在位圖圖像中創建具有透明度的邊角。
像下面
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<!-- you can use any color you want I used here gray color-->
<solid android:color="#ABABAB"/>
<corners android:radius="10dp"/>
</shape>
應用以此爲背景按鈕創建繪製文件夾中的XML文件,你想要做的角落一輪。
或者你可以使用不同的半徑每一個角落,像下面
android:bottomRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp"
你可以縮短角落到android:radius =「10dp」,這將適用於所有 – 2012-06-30 19:01:34
這是不是一個完整的解決方案,因爲它不支持各種按鈕狀態(按下,關注,默認)。爲了更好的解決方案,請參閱http://stackoverflow.com/questions/9334618/roundedbutton-android – JosephL 2013-03-21 21:10:15
@BenSimpson,你會看到,當你只放一條線而不是定義每一個角落半徑單獨。 – 2013-05-29 05:27:25
在繪製文件夾
創建shape.xml像shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke android:width="2dp"
android:color="#FFFFFF"/>
<gradient
android:angle="225"
android:startColor="#DD2ECCFA"
android:endColor="#DD000000"/>
<corners
android:bottomLeftRadius="7dp"
android:bottomRightRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp" />
</shape>
和myactivity.xml
可以使用
<Button
android:id="@+id/btn_Shap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Shape"
android:background="@drawable/shape"/>
我發現的簡單方法是在可繪製文件夾中創建一個新的xml文件,然後將按鈕背景指向該xml文件。繼承人的代碼我使用:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#ff8100"/>
<corners android:radius="5dp"/>
</shape>
要在自定義可繪製背景中恢復材質主題漣漪效果,請在按鈕視圖中添加'android:foreground =「?attr/selectableItemBackground」''。請參閱https://stackoverflow.com/questions/38327188/android-setting-background-color-of-button-loses-ripple-effect – 2018-02-13 17:58:12
創建可繪製文件夾rounded_btn.xml文件...
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/#FFFFFF"/>
<stroke android:width="1dp"
android:color="@color/#000000"
/>
<padding android:left="1dp"
android:top="1dp"
android:right="1dp"
android:bottom="1dp"
/>
<corners android:bottomRightRadius="5dip" android:bottomLeftRadius="5dip"
android:topLeftRadius="5dip" android:topRightRadius="5dip"/>
</shape>
,並使用this.xml文件作爲按鈕的背景
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/rounded_btn"
android:text="Test" />
<Button
android:id="@+id/buttonVisaProgress"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"
android:background="@drawable/shape"
android:onClick="visaProgress"
android:drawableTop="@drawable/ic_1468863158_double_loop"
android:padding="10dp"
android:text="Visa Progress"
android:textColor="@android:color/white" />
形狀。XML
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="14dp" />
<gradient
android:angle="45"
android:centerColor="#1FA8D1"
android:centerX="35%"
android:endColor="#060d96"
android:startColor="#0e7e1d"
android:type="linear" />
<padding
android:bottom="0dp"
android:left="0dp"
android:right="0dp"
android:top="0dp" />
<size
android:width="270dp"
android:height="60dp" />
<stroke
android:width="3dp"
android:color="#000000" />
創建文件myButton.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/colorButton"/>
<corners android:radius="10dp"/>
</shape>
添加到您的按鈕
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background">@drawable/myButton
/>
此鏈接有你需要的所有信息。 Here
Shape.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#EAEAEA"/>
<corners android:bottomLeftRadius="8dip"
android:topRightRadius="8dip"
android:topLeftRadius="1dip"
android:bottomRightRadius="1dip"
/>
</shape>
和main.xml中
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello Android from NetBeans"/>
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nishant Nair"
android:padding="5dip"
android:layout_gravity="center"
android:background="@drawable/button_shape"
/>
</LinearLayout>
這應該給你你想要的結果。
祝你好運
這很簡單。創建一個如下所示的XML文件。將其設置爲按鈕的背景。如果需要更多按鈕的曲線,請將radius屬性更改爲您的願望。
button_background.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/primary" />
<corners android:radius="5dp" />
</shape>
設置背景您的按鈕:
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_background"/>
- 1. 如何繞一個按鈕的角落
- 2. 如何使PolyLineSegment的角落變圓?
- 3. 按鈕的圓角
- 4. 如何讓一個按鈕留在角落的主屏幕上?
- 5. 圓桌角落
- 6. 在角落裏的按鈕?
- 7. 屏幕角落的按鈕
- 8. 圓角按鈕ASP.NET
- 9. 如何用IB創建角落按鈕?
- 10. UINavigationBar圓角的兩個角落
- 11. 如何使一個矩形的一個角落的圓形反彈 - 處理
- 12. Flex中按鈕的圓形特定角落
- 13. Android:不改變背景顏色的圓形按鈕角落
- 14. CSS3倒圓角落
- 15. 如何使用jQuery .slideUp()使一個浮動按鈕出現在角落?
- 16. 如何使webview與圓角矩形的角落?
- 17. 使用CSS切換按鈕角落
- 18. 圖像圓角按鈕JavaFX
- 19. 圓角圖像只有某些角落與calayer的角落Radius
- 20. 如何讓我的圓角按鈕有被按下的感覺?
- 21. 僅限於角落的圓角陰影
- 22. CSS圓角表的角落 - 問題
- 23. Android:按鈕在地圖的角落
- 24. CoreGraphics圓形角落厚度
- 25. box2d圓體卡在角落
- 26. Objective C,如何使UIImageView中的圖片的角落變圓?
- 27. 如何讓角落只在CSS的邊界頂部圓角?
- 28. 如何使用畫布繪製圓角按鈕?
- 29. 如何使用css文件創建圓角按鈕?
- 30. 如何在libgdx中創建一個簡單的圓角矩形按鈕?
蘇尼爾:哥們,這是一個誠實的問題。我也搜索了很多自己,想出一個合理的答案 - 這並不簡單。我期待Android SDK在Button對象本身提供這個屬性... – 2011-11-21 18:25:06
看看這個:http://nishantvnair.wordpress.com/2010/11/09/customize-button-in-android/ – Lavanya 2011-05-19 06:19:24
檢查這個out http://www.gadgetsaint.com/tips/rounded-corners-views-layouts-android/#.WPz2QVN97BI – ASP 2017-04-23 18:50:07