android内置测拉栏DrawerLayout的使用

来源:互联网 发布:淘宝客服礼貌用语 编辑:程序博客网 时间:2024/05/18 22:42

第一步导库配置
这个空间是widget包下的一个控件加入依赖
compile ‘com.android.support:appcompat-v7:25.0.1’
第二步使用
首先在住布局main_activity.xml。

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"        android:id="@+id/dl"        android:layout_width="match_parent"        android:layout_height="match_parent">        <RelativeLayout            android:id="@+id/activity_main"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:paddingBottom="@dimen/activity_vertical_margin"            android:paddingLeft="@dimen/activity_horizontal_margin"            android:paddingRight="@dimen/activity_horizontal_margin"            android:paddingTop="@dimen/activity_vertical_margin"            tools:context="org.cocos2d.ndkdemo.MainActivity">            <Button                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:onClick="testNdk" />        </RelativeLayout>        //end 表示滑动菜单栏在右边 ,start表示滑动测拉栏在左边        <LinearLayout            android:id="@+id/container"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="end"            android:orientation="vertical" />    </android.support.v4.widget.DrawerLayout>

在代码中使用
首先右测拉栏内容,放置一个RightProductFragment

public class RightProductFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
TextView tv=new TextView(getActivity());
Bundle bundle= setArguments();
String product= bundle.getString(“product”);
tv.setText(product);
return tv;
}

public static RightProductFragment getInstance(String args) {    RightProductFragment mProductFragment = new RightProductFragment();    Bundle bundle = new Bundle();    bundle.putString("product", args);    mProductFragment.setArguments(bundle);    return mProductFragment;}

在MainActivity中的使用

public class MainActivity extends Activity {

private DrawerLayout drawerlayout;@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    drawerlayout = (DrawerLayout) findViewById(R.id.dl);    //禁止测拉栏通过手势滑动打开   // drawerlayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);    getFragmentManager().beginTransaction().replace(R.id.container, RightProductFragment.getInstance("hhahah")).commit();}public void testNdk(View view) {//表示点击按钮开启测拉栏    drawerlayout.openDrawer(Gravity.RIGHT);}

}
“`

0 0
原创粉丝点击