标签到浮动按钮

时间:2017-01-05 21:42:12

标签: android floating-action-button

我正在尝试在FAB一侧插入标签......
我搜索了一下,我找到了LinearLayout和TextView的方法,但我无法做到..
到目前为止我已写过这个,但它并没有显示我甚至没有按钮..

<?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"
tools:context="com.example.nigi.myapplication2.MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    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/content_main" />
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:visibility="invisible">
<TextView
    android:text="TextView"
    android:layout_width="70dp"
    android:layout_height="15dp"
    android:id="@+id/textView3"
    android:layout_weight="1" />
<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"
    app:backgroundTint="@color/colorFAB2"
    app:elevation="6dp"
    app:pressedTranslationZ="12dp"
    android:src="@android:drawable/ic_lock_silent_mode" />
</LinearLayout>
<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/colorFAB1"
    app:pressedTranslationZ="12dp"
    android:src="@android:drawable/ic_menu_save"/>
<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="338dp"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|right"
    app:elevation="6dp"
    app:backgroundTint="@color/colorAccent"
    app:pressedTranslationZ="12dp"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_menu_add"
    app:layout_anchorGravity="top|left" />
</android.support.design.widget.CoordinatorLayout>

你有什么想法,关于什么是错的?谢谢!!

更新

昨天我有点累了,我没有看到很多想法 感谢您的评论!!
所以,我重写了LinearLayout部分,并对java文件进行了一些更改,但仍然存在一些问题:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/linear1"

    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:visibility="invisible"

    android:layout_marginBottom="160dp"
    android:layout_gravity="bottom|end"
    android:layout_marginRight="@dimen/fab_margin">

<TextView
    android:text="TextView"
    android:layout_width="70dp"
    android:layout_height="15dp"
    android:id="@+id/textView3"
    android:layout_weight="1" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:backgroundTint="@color/colorFAB2"
    app:elevation="6dp"
    app:pressedTranslationZ="12dp"
    android:src="@android:drawable/ic_lock_silent_mode" />

</LinearLayout>

并在java文件中:

public class MainActivity extends AppCompatActivity implement View.OnClickListener{
private Boolean isFabOpen = false;
private FloatingActionButton fab,fab1,fab2;
private Animation fab_open,fab_close,rotate_forward,rotate_backward;
LinearLayout linear1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    fab = (FloatingActionButton)findViewById(R.id.fab);
    fab1 = (FloatingActionButton)findViewById(R.id.fab1);
    fab2 = (FloatingActionButton)findViewById(R.id.fab2);

    linear1 = (LinearLayout)findViewById(R.id.linear1);

    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);
    linear1.setOnClickListener(this);
}

public void onClick(View v) {
    int id = v.getId();
    switch (id){
        case R.id.fab:

            animateFAB();
            break;
        case R.id.fab1:

            Log.d("Raj", "Fab 1");
            break;
        case R.id.fab2:

            Log.d("Raj", "Fab 2");
            break;
    }
}

public void animateFAB(){

    if(isFabOpen){

        fab.startAnimation(rotate_backward);
        fab1.startAnimation(fab_close);
        linear1.startAnimation(fab_close);
        fab1.setClickable(false);
        fab2.setClickable(false);
        isFabOpen = false;
        Log.d("Raj", "close");

    } else {

        fab.startAnimation(rotate_forward);
        fab1.startAnimation(fab_open);
        linear1.startAnimation(fab_open);
        fab1.setClickable(true);
        fab2.setClickable(true);
        isFabOpen = true;
        Log.d("Raj","open");

    }
}
}


and the screenshot after the changes is:

任何想法有什么不对?谢谢!!

1 个答案:

答案 0 :(得分:0)

也许它是无关的,但这LinearLayout似乎错了:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:visibility="invisible">

你有一个看不见的视图,它占据了整个屏幕并没有id,所以我想你不会把它变成可见的。