将导航抽屉片段添加到活动

时间:2016-10-14 07:19:24

标签: android android-fragments android-navigation-drawer

我想将我的导航抽屉片段添加到活动中,但我希望活动布局"背后"导航抽屉将保持不变。

我得到了什么 enter image description here

没有打开导航抽屉
Without open Navigation Drawer<br>

我想要什么 enter image description here

导航抽屉片段活动XML

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <RelativeLayout
        android:layout_width="280dp"
        android:layout_height="match_parent"
        android:id="@+id/drawerPane"
        android:layout_gravity="start">


        <RelativeLayout
            android:id="@+id/logoBox"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:background="@color/material_blue_grey_800"
            android:padding="8dp" >

            <ImageView
                android:id="@+id/g4aLogo"
                 android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_marginTop="15dp"
                android:layout_centerInParent="true"/>

        </RelativeLayout>

        <ListView
            android:id="@+id/settingsList"
            android:layout_width="280dp"
            android:layout_height="match_parent"
            android:layout_below="@+id/logoBox"
            android:choiceMode="singleChoice"
            android:background="#ffffffff" />
    </RelativeLayout>
</android.support.v4.widget.DrawerLayout>

活动XML

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/activity_main"
    android:orientation="vertical"
    android:weightSum="10">


    <LinearLayout
        android:layout_weight="1"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:id="@+id/searchBarLay"
        android:focusable="true"
        android:focusableInTouchMode="true">


//CONTENTS and layouts...


</LinearLayout>
</LinearLayout>

实施导航抽屉时的活动代码部分

private void initSettingsDrawer() {
        Button settingsDrawerButton =(Button)findViewById(R.id.settingsDrawer);
        settingsDrawerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SettingsDrawer settingsDrawer = new SettingsDrawer();
                FragmentManager fragmentManager = getSupportFragmentManager();
                fragmentManager.beginTransaction().add(R.id.activity_main, settingsDrawer).commit();
            }
        });
    }

2 个答案:

答案 0 :(得分:0)

android.support.v4.widget.DrawerLayout应该是父/根布局。你的xml应该是这样的,

@Component({
    selector: 'input-text2',
    templateUrl: './input-text2.component.html',
    styleUrls: ['/input-base2.scss', './input-text2.component.scss'],
    providers: [
        { provide: NG_VALUE_ACCESSOR, multi: true, useExisting: InputText2Component }
    ]
})
export class InputText2Component extends InputBase2 implements ControlValueAccessor, OnInit {

    private ngControl: NgControl;


    value: string;
    valueChange: (value: any) => void;
    _onTouched: (value: any) => void;

    constructor(private injector: Injector) {
        super();
    }

    ngOnInit(): void {
        this.ngControl = this.injector.get(NgControl);
    }

    ..//other methods
}

答案 1 :(得分:0)

将此库添加到build.gradle文件

compile(&#39; com.mikepenz:materialdrawer:5.1.3@aar'){         transitive = true}

然后在您想要导航抽屉的Activity中添加此代码。

public class MainActivity extends AppCompatActivity {
private Drawer result = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

   Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    AccountHeader headerResult = new AccountHeaderBuilder()
            .withActivity(this)
           // .withHeaderBackground(R.drawable.nav_header)

            .addProfiles(
                    new ProfileDrawerItem().withEmail("abc@gmail.com").withIcon(getResources().
                            getDrawable(R.mipmap.ic_launcher))
            )
            .withOnAccountHeaderListener(new AccountHeader.OnAccountHeaderListener() {
                @Override
                public boolean onProfileChanged(View view, IProfile profile, boolean currentProfile) {
                    return false;
                }
            })
            .build();

    result = new DrawerBuilder()
            .withActivity(this)
            .withToolbar(toolbar)
            .withActionBarDrawerToggleAnimated(true)
            .withAccountHeader(headerResult)
            .build();
    result.addItem(new PrimaryDrawerItem()
            .withName(("Home")).withIcon(R.mipmap.ic_launcher).
                    withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                        @Override
                        public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
                            return false;
                        }
                    }));
    result.addItem(new PrimaryDrawerItem()
            .withName(("Settings")).withIcon(R.mipmap.ic_launcher).
                    withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                        @Override
                        public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
                            return false;
                        }
                    }));

}

}