在Android中向浮动按钮添加文本

时间:2016-07-25 12:29:42

标签: java android floating-action-button

我创建了一个简单的浮动菜单。浮动菜单工作正常。

但我想在菜单的左侧添加一些文字,如下图所示。

enter image description here

有人可以帮助我在浮动按钮之前添加一些文字,就像我展示的图像一样。

浮动菜单xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/activity_ticket_details" />


    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="230dp"
        android:layout_gravity="bottom|end"
        android:layout_marginRight="@dimen/fab_margin"
        android:visibility="invisible"
        app:backgroundTint="@color/colorPrimary"
        app:elevation="6dp"
        app:pressedTranslationZ="12dp"
        android:src="@drawable/icon_floating_forward" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="160dp"
        android:layout_gravity="bottom|end"
        android:layout_marginRight="@dimen/fab_margin"
        android:visibility="invisible"
        app:backgroundTint="@color/colorPrimary"
        app:elevation="6dp"
        app:pressedTranslationZ="12dp"
        android:src="@drawable/icon_floating_note" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="90dp"
        android:layout_gravity="bottom|end"
        android:layout_marginRight="@dimen/fab_margin"
        android:visibility="invisible"
        app:elevation="6dp"
        app:backgroundTint="@color/colorPrimary"
        app:pressedTranslationZ="12dp"
        android:src="@drawable/icon_floating_replay" />
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        app:elevation="6dp"
        app:backgroundTint="@color/colorPrimaryDark"
        app:pressedTranslationZ="12dp"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/icon_floating_replay" />

</android.support.design.widget.CoordinatorLayout>

活动类

public class TicketDetails extends AppCompatActivity implements View.OnClickListener{
    TextView tv_ticket_id;
    ImageView back_arrow;
    TextView ticket_status;
    TextView ticket_overdue;
    TextView ticket_subject;

    Context context;
    ProgressDialog pDialog;
    public String BASEURL;
    public ApplicationEnvironmentURL applicationEnvironment;

    private Boolean isFabOpen = false;
    private FloatingActionButton fab,fab1,fab2,fab3;
    private Animation fab_open,fab_close,rotate_forward,rotate_backward;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_ticket_details);
        setContentView(R.layout.activity_ticket_details_floating);

        applicationEnvironment = new ApplicationEnvironmentURL(this.context);
        context = this.getApplicationContext();

        Intent intent = getIntent();
        String ticket_id = intent.getStringExtra("ticket_id");


        back_arrow = (ImageView) findViewById(R.id.IV_td_leftarrow_bar);
        back_arrow.setOnClickListener(onClickListener);

        /*********************** Floating Icon *****************************************************/
        fab = (FloatingActionButton)findViewById(R.id.fab);
        fab1 = (FloatingActionButton)findViewById(R.id.fab1);
        fab2 = (FloatingActionButton)findViewById(R.id.fab2);
        fab3 = (FloatingActionButton)findViewById(R.id.fab3);
        fab_open = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fab_open);
        fab_close = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fab_close);
        rotate_forward = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.rotate_forward);
        rotate_backward = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.rotate_backward);
        fab.setOnClickListener(this);
        fab1.setOnClickListener(this);
        fab2.setOnClickListener(this);
        fab3.setOnClickListener(this);
        /*******************************************************************************************/


    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id){
            case R.id.fab:
                animateFAB();
                break;
            case R.id.fab1:
                break;
            case R.id.fab2:
                break;
            case R.id.fab3:
                break;
        }
    }

    public void animateFAB(){

        if(isFabOpen){
            fab.startAnimation(rotate_backward);
            fab1.startAnimation(fab_close);
            fab2.startAnimation(fab_close);
            fab3.startAnimation(fab_close);
            fab1.setClickable(false);
            fab2.setClickable(false);
            fab3.setClickable(false);
            isFabOpen = false;
            Log.d("ZF-Fab", "close");

        } else {

            fab.startAnimation(rotate_forward);
            fab1.startAnimation(fab_open);
            fab2.startAnimation(fab_open);
            fab3.startAnimation(fab_open);
            fab1.setClickable(true);
            fab2.setClickable(true);
            fab3.setClickable(true);
            isFabOpen = true;
            Log.d("ZF-Fab","open");

        }
    }

..............more

2 个答案:

答案 0 :(得分:0)

如您所知,FAB需要在其视图上显示drawable以便在其上显示文字,您需要在其上创建带有文字的drawble,然后使用以下代码创建。

 FloatingActionButton fab3 = (FloatingActionButton)findViewById(R.id.fab3);
 fab3.setDrawble or source there must a method

 TextDrawable trxtDrawable = new TextDrawable("AB");
 fab3.setImageDrawable(trxtDrawable);

<强> TextDrawable:

public class TextDrawable extends Drawable {

    private final String text;
    private final Paint paint;

    public TextDrawable(String text) {

        this.text = text;

        this.paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setTextSize(22f);
        paint.setAntiAlias(true);
        paint.setFakeBoldText(true);
        paint.setShadowLayer(6f, 0, 0, Color.BLACK);
        paint.setStyle(Paint.Style.FILL);
        paint.setTextAlign(Paint.Align.LEFT);
    }

    @Override
    public void draw(Canvas canvas) {
        canvas.drawText(text, 0, 0, paint);
    }

    @Override
    public void setAlpha(int alpha) {
        paint.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
        paint.setColorFilter(cf);
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }
}

答案 1 :(得分:0)

这是我根据上面@Sohail Zahid 开始的代码最终使用的,但我无法按照他的评论/让它工作关于如何使用 ImageView 添加图像所以我最终使用了代替位图。

另外,我无法让几个方法工作,所以我传递了在构造函数中调用它的活动。

我对此非常陌生,所以如果我没有遵循任何最佳实践,我很抱歉,如果有更好的方法,我很乐意进一步修改代码。

创建类时,通过右键单击您的 java>com.example.appname 并选择 New>Java Class>Class (Name: TextDrawable)

1xxxxxxx

我得到的结果是这样的: enter image description here