[英]How to programmatically create a Navigation Drawer?
I need create activity with navigation drawer only with java without xml.我只需要使用没有 xml 的 java 创建带有导航抽屉的活动。 but this way doesn't work for me.
但这种方式对我不起作用。 Is it possible to create and add to app Navigation Drawer without using xml?
是否可以在不使用 xml 的情况下创建并添加到应用程序导航抽屉? Every example i saw had an additional xml file with layout.
我看到的每个示例都有一个带有布局的额外 xml 文件。 I searched very much but I did not get any results.
我搜索了很多,但没有得到任何结果。 Please help me.
请帮我。 in this way navigation drawer always open on contentLayout and not moving with finger or swiping !
这样导航抽屉总是在 contentLayout 上打开,而不是用手指或滑动移动!
my activity code:我的活动代码:
import androidx.drawerlayout.widget.DrawerLayout;
public class LaunchActivity extends Activity {
private Context context;
private DrawerLayout drawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = getApplicationContext();
//DrawerLayout
drawerLayout = new DrawerLayout(context);
drawerLayout.setLayoutParams(new DrawerLayout.LayoutParams(DrawerLayout.LayoutParams.MATCH_PARENT, DrawerLayout.LayoutParams.MATCH_PARENT));
//ContentLayout
LinearLayout contentLayout = new LinearLayout(context);
contentLayout.setOrientation(LinearLayout.VERTICAL);
contentLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
//CustomNavigationView
RelativeLayout drawerView = new RelativeLayout(context);
drawerView.setLayoutParams(new RelativeLayout.LayoutParams(AndroidUtilities.dp(280), RelativeLayout.LayoutParams.MATCH_PARENT));
drawerView.setGravity(Gravity.START);
//Add View to ContentLayout
TextViewFont textView = new TextViewFont(context);
textView.setText("=");
textView.setTextColor(Color.BLACK);
textView.setTextSize(20);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
drawerLayout.openDrawer(Gravity.START);
}
});
contentLayout.addView(textView);
//Add View to CustomNavigationView
RelativeLayout drawerHeaderView = new RelativeLayout(context);
drawerHeaderView.setBackground(getResources().getDrawable(R.drawable.drawer_header));
drawerHeaderView.setGravity(Gravity.BOTTOM);
drawerHeaderView.setLayoutParams(new RelativeLayout.LayoutParams(AndroidUtilities.dp(280), AndroidUtilities.dp(170)));
ImageView imageView = new ImageView(context);
imageView.setImageDrawable(getResources().getDrawable(R.drawable.user));
imageView.setBaselineAlignBottom(true);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(AndroidUtilities.dp(90), AndroidUtilities.dp(90));
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
drawerHeaderView.addView(imageView, params);
drawerView.addView(drawerHeaderView);
//Add All to DrawerLayout
drawerLayout.addView(contentLayout, new DrawerLayout.LayoutParams(DrawerLayout.LayoutParams.MATCH_PARENT, DrawerLayout.LayoutParams.MATCH_PARENT));
drawerLayout.addView(drawerView, new RelativeLayout.LayoutParams(AndroidUtilities.dp(280), RelativeLayout.LayoutParams.MATCH_PARENT));
setContentView(drawerLayout);
}
}
你可以使用这个库,然后在 BaseActivity 中覆盖 setContentView() 并在那里初始化抽屉构建器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.