如何在NavigationView的DrawerLayout中显示弹出TextView

时间:2016-12-01 09:04:05

标签: android drawerlayout

我尝试创建一个像Google Hangout appen一样弹出底部的TextView。 Google环聊应用会在几秒钟内显示弹出窗口,并以xxx身份登录。

我有这个经典的导航抽屉布局

<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.port.android.ui.ActivityMain">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include
            android:id="@+id/toolbar"
            layout="@layout/tool_bar" />

        <FrameLayout
            android:id="@+id/frame"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">
        </FrameLayout>

        <TextView
            android:id="@+id/popup_logged_in"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/PrimaryColor"
            android:layout_alignParentBottom="true"
            android:padding="15dp"
            android:textSize="14sp"
            android:visibility="gone"
            android:gravity="center"
            android:textColor="@android:color/white"
            android:textAllCaps="true"
            android:textStyle="bold" />

    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/navigation_view_header"
        app:menu="@menu/menu_navigation">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:padding="20dp"
            android:orientation="horizontal">
            <Button
                android:id="@+id/btn_exit"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="exitAddress"
                android:text="Exit Address"/>

        </LinearLayout>
    </android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>

如你所见,我在TextView内放了一个LinearLayout,当我view.animate()时它会显示出来,但它会推动布局,我希望它能保持在最顶层。 所以我尝试RelativeLayout,但这使得工具栏消失了。

AppCompatActivity如果有帮助:

@Override
protected void onCreate(Bundle savedInstanceState) {
    if (Application.getInstance().isClosing())
        finish();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    popUpThreadPoolFactory = new PopUpThreadPoolFactory(this);
    fragManager = getSupportFragmentManager();
    mMainAdapter = new ActivityMainAdapter(this);
    // Initializing Toolbar and setting it as the actionbar
    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);
    //Initializing NavigationView
    mNavigationView = (NavigationView) findViewById(R.id.navigation_view);
    mNavigationView.setNavigationItemSelectedListener(this);
    // Initializing Drawer Layout and ActionBarToggle
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
    ((Button) findViewById(R.id.btn_exit)).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            FirebaseManager.getInstance().exitCurrentAddress();
        }
    });

    mActionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.openDrawer, R.string.closeDrawer) {
        @Override
        public void onDrawerClosed(View drawerView) {
            // Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
            super.onDrawerClosed(drawerView);
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            // Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank
            super.onDrawerOpened(drawerView);
        }
    };
    //Setting the actionbarToggle to drawer layout
    mDrawerLayout.addDrawerListener(mActionBarDrawerToggle);
    //calling sync state is necessay or else your hamburger icon wont show up
    mActionBarDrawerToggle.syncState();
    // Initialize Firebase Analytics
    mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);

    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    client2 = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}

@Override
protected void onStart() {
    super.onStart();
    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    client2.connect();
    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    Action viewAction = Action.newAction(
            Action.TYPE_VIEW, // TODO: choose an action type.
            "ActivityMain Page", // TODO: Define a title for the content shown.
            // TODO: If you have web page content that matches this app activity's content,
            // make sure this auto-generated web page URL is correct.
            // Otherwise, set the URL to null.
            Uri.parse("http://host/path"),
            // TODO: Make sure this auto-generated app URL is correct.
            Uri.parse("android-app://com.port.android/http/host/path")
    );
    AppIndex.AppIndexApi.start(client2, viewAction);
}

@Override
protected void onResume() {
    super.onResume();
    MyLog.i(TAG, "onResume");
    Application.getInstance().addUIListener(OnConnectionStateListener.class, this);
    Application.getInstance().addUIListener(OnExitAddressListener.class, this);
    Application.getInstance().addUIListener(OnUserSignedInListener.class, this);
    // update state
    if (Application.getInstance().isInitialized() && !Application.getInstance().isClosing()) {
        MyLog.i(TAG, "Application isInitialized activating adapter");
        mainAdapterOnChange();
    } else
        MyLog.i(TAG, "Wait for loading or closing");
}

1 个答案:

答案 0 :(得分:2)

我认为您正在搜索 SnackBar。。如果这是你正在尝试的,那么这是一个例子。

在您想要显示此内容的活动中,绑定您的父布局。假设:
在您的activity.xml中

<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:id="@+id/cd"
tools:context="com.example.ayyappaboddupalli.dragmaker.MainActivity">

在您的activity.java类中:

    CoordinatorLayout cd=(CoodinatorLayour)findViewById(R.id.cd);

现在,如果您想在点击按钮时显示该弹出窗口:

    btn.setOnClickListener(new onClickListener()

    public void onClick(View v)
   {
   Snackbar.make(cd,"text you want to show",Snackbar.LENGTH_LONG).show();
    }   
    );