2017-08-11 83 views
1

我需要创建一个带白色边框的FAB,并填充纯色(蓝色或灰色)。FloatingActionButton的白色边框

XML

<android.support.design.widget.FloatingActionButton 
     android:id="@+id/myFab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center"/> 

enter image description here

我试图接受的解决方案中How to change android design support library FAB Button border color?建议。但没有工作(没有添加边框)。

任何帮助将非常感激。

+1

你可以编辑你的问题,包括你的意思是“它没有工作” –

+0

@ AjilO.What我的意思是,它没有添加边框。请参阅我通过此更改创建的项目 - https://drive.google.com/open?id=0BxEa4zO6gm2FT1hkemtfUC1vSW8 – coder

回答

3
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/rl_content_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@android:color/black"> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:background="@drawable/circle_white_border" 
     android:padding="3dp"> 

     <android.support.design.widget.FloatingActionButton 
      android:id="@+id/fab_btn" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:elevation="0dp" 
      app:elevation="0dp" 
      app:fabSize="normal" /> 
    </LinearLayout> 

</RelativeLayout> 

背景的浮动操作按钮

in the drawable fab_border.xml 

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:innerRadius="0dp" 
    android:shape="ring" 
    android:thicknessRatio="2" 
    android:useLevel="false"> 

    <stroke 
     android:width="4dp" 
     android:color="@android:color/white" /> 
</shape> 
+0

感谢您的帮助!这个解决方案在手机中工作正常但在平板电脑中增加了额外的填充。 – coder

+0

问题不在平板电脑中。在棒棒堂操作系统中,FAB有一个额外的填充。这可以通过为FloatingActionButton添加 app:elevation =“0dp” app:pressedTranslationZ =“0dp” 来解决。所以我接受了这个答案。非常感谢! – coder

+0

请注意,背景可绘制设置为周围的线性布局,而不是晶圆厂本身。否则它不适合我。 – DanD

0

drawable circle_white_border.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:innerRadius="0dp" 
    android:shape="ring" 
    android:thicknessRatio="2" 
    android:useLevel="false" > 
    <solid android:color="@android:color/transparent" /> 

    <stroke 
     android:width="3dp" 
     android:color="@android:color/white" /> 
</shape> 

浮动操作按钮布局

<android.support.design.widget.FloatingActionButton 
     android:id="@+id/myFab" 
     android:background="@drawable/circle_white_border" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center"/> 
+0

感谢您的帮助,我的朋友!该解决方案对我无效。请查看附带的解决方案 - https://drive.google.com/open?id=0BxEa4zO6gm2FT1hkemtfUC1vSW8 – coder

0

您可以创建此编程为:

ImageButton iV = (ImageButton) findViewById(R.id.myFab); 
    int strokeWidth = 5; 
    int strokeColor = Color.parseColor("#03dc13"); 
    int fillColor = Color.parseColor("#ff0000"); 
    GradientDrawable gD = new GradientDrawable(); 
    gD.setColor(fillColor); 
    gD.setShape(GradientDrawable.OVAL); 
    gD.setStroke(strokeWidth, strokeColor); 
    iV.setBackground(gD); 

根据您的要求更改颜色。