保持堆叠视图在底部

时间:2016-09-01 10:30:53

标签: android android-activity fragment navigation-drawer

您好我使用导航抽屉,片段替换为帧但我想保持底部空间更改片段在空白区域。我怎样才能做到这一点?

<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=".MainActivity">

    <FrameLayout
        android:id="@+id/frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <View
            android:layout_width="match_parent"
            android:layout_height="70dp"
            android:layout_gravity="bottom"
            android:background="@color/colorPrimary" />
    </FrameLayout>


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

</android.support.v4.widget.DrawerLayout>

MainActivity.java

包panayiotis.mysmarthome;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import panayiotis.mysmarthome.fragments.Dashboard;
import panayiotis.mysmarthome.fragments.Home;
import panayiotis.mysmarthome.eventbus.MessageEvent;
import panayiotis.mysmarthome.libraries.mqtt.ImqttCallback;

public class MainActivity extends AppCompatActivity implements ImqttCallback{

    //Defining Variables
    public DrawerLayout drawerLayout;
    private ActionBarDrawerToggle drawerToggle;
    private NavigationView navigationView;

    private Handler mHandler;
    private Runnable mPendingRunnable;

    private Fragment selectedFragment;

    private Snackbar snackbar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mHandler = new Handler(getMainLooper());
        initInstances();
    }

    private void initInstances() {
        //Initializing NavigationView
        navigationView = (NavigationView) findViewById(R.id.navigation_view);

        //Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

            // This method will trigger on item Click of navigation menu
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {


                //Checking if the item is in checked state or not, if not make it in checked state
                if (!menuItem.isChecked())
                    menuItem.setChecked(true);
                else {
                    //Closing drawer on item click
                    drawerLayout.closeDrawers();
                    return true;
                }

                selectedFragment = null;

                //Check to see which item was being clicked and perform appropriate action
                switch (menuItem.getItemId()) {
                    case R.id.dashboard:
                        selectedFragment = new Dashboard();
                        break;
                    case R.id.home:
                        selectedFragment = new Home();
                        break;
                    default:
                        Toast.makeText(getApplicationContext(), "Somethings Wrong", Toast.LENGTH_SHORT).show();
                        break;

                }

                mPendingRunnable = new Runnable() {
                    @Override
                    public void run() {
                        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                        fragmentTransaction.replace(R.id.frame, selectedFragment);
                        fragmentTransaction.commit();
                    }
                };

                //Closing drawer on item click
                drawerLayout.closeDrawers();
                return true;
            }
        });

        // Initializing Drawer Layout and ActionBarToggle
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer);

        drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.drawer_open, R.string.drawer_close) {
            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);

                // If mPendingRunnable is not null, then add to the message queue
                if (mPendingRunnable != null) {
                    mHandler.post(mPendingRunnable);
                    mPendingRunnable = null;
                }
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
            }
        };

        //Setting the actionbarToggle to drawer layout
        drawerLayout.addDrawerListener(drawerToggle);

        selectedFragment = new Dashboard();

        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.frame, selectedFragment);
        fragmentTransaction.commit();
    }


    @Override
    public void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        drawerToggle.syncState();
    }

    @Override
    protected void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
        snackbar = Snackbar
                .make(drawerLayout, "Connecting...", Snackbar.LENGTH_INDEFINITE);

        snackbar.show();
        MySmartHome.getInstance().getMqttClient().connect(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MySmartHome.getInstance().getMqttClient().disconnect();
    }

    /**
     * Eventbus function
     */
    @Subscribe
    public void onEventBusReceived(MessageEvent event) {
        if (event.getType() == MessageEvent.Type.LOG_OUT) {
            logout();
        }
    }

    private void logout() {
        MySmartHome.getInstance().getDataManager().deleteAllData();
        Intent intent = new Intent(MainActivity.this, LoginActivity.class);
        startActivity(intent);
        finish();
    }


    /**
     * ImqttCallback
     */
    @Override
    public void connected() {
        snackbar.dismiss();
        snackbar = Snackbar
                .make(drawerLayout, "Connected", Snackbar.LENGTH_SHORT);

        snackbar.show();
    }

    @Override
    public void connectionFailed() {

    }
}

3 个答案:

答案 0 :(得分:1)

您可以通过背景drawable和paddingBottom建立此布局。如下。 layout xml:

<android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        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=".MainActivity">

    <FrameLayout
            android:id="@+id/frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingBottom="70dp"
            android:background="@drawable/bg_frame"/>

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

</android.support.v4.widget.DrawerLayout>

bg_frame.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:height="70dp" android:gravity="bottom">
        <shape android:shape="rectangle">
            <solid android:color="@color/colorPrimary"/>
        </shape>
    </item>
</layer-list>

答案 1 :(得分:0)

这应该可以保证金吗? layout_marginBottom =

答案 2 :(得分:0)

使用此布局。您的问题将得到解决。

<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=".MainActivity">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <View
            android:id="@+id/fixedView"
            android:layout_alignParentBottom="true"
            android:layout_width="match_parent"
            android:layout_height="70dp"
            android:layout_gravity="bottom"
            android:background="@color/colorPrimary" />

        <FrameLayout
            android:layout_above="@id/fixedView"
            android:id="@+id/frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </RelativeLayout>

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

</android.support.v4.widget.DrawerLayout>