Мне нужно создать активность с помощью навигационного ящика только с Java без XML.но этот способ не работает для меня.Можно ли создавать и добавлять в приложение Navigation Drawer без использования xml?Каждый пример, который я видел, имел дополнительный XML-файл с макетом.Я искал очень много, но я не получил никаких результатов.Пожалуйста, помогите мне.таким образом навигационный ящик всегда открыт на contentLayout и не двигается пальцем или проведением пальцем!
мой код активности:
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);
}
}