问题
I'm using androidx for development and I'm trying to use the NavigationView
and since the findViewById
doesn't usually work when you need it to, I'm trying to do everything in Java Code. So, I'm trying to get this NavigationView
to appear, but this seems to be missing something:
public class MainActivity extends AppCompatActivity {
Activity_Animation001_Layout animation001_layout;
Animation_Activity002 animLay2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DrawerLayout mDrawer = new DrawerLayout(this);
mDrawer.setLayoutParams(new DrawerLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
LinearLayout mLinearLayout = new LinearLayout(this);
mLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
mLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
TextView mTextView = new TextView(this);
mTextView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
mTextView.setText("something");
mTextView.setTextSize(30);
mLinearLayout.addView(mTextView);
NavigationView mNavView = new NavigationView(this);
NavigationView.LayoutParams mNavLayParam = new NavigationView.LayoutParams(250, ViewGroup.LayoutParams.MATCH_PARENT);
mNavLayParam.gravity= Gravity.RIGHT;
mNavView.setLayoutParams(mNavLayParam);
mDrawer.addView(mLinearLayout);
mDrawer.addView(mNavView);
// animLay2 = new Animation_Activity002(this);
setContentView(mDrawer);
}
The Drawer
, LinearLayout
and TextView
seem to be appearing, but I make the NavigationView
to appear when I swipe left from the right edge of the screen. How should we add and instantiate Views(or the NavigationView
in this case)?
回答1:
You are almost in the right path, you need to change the mNavView gravity to the Gravity.START, and use DrawerLayout.LayoutParams
for the NavigationView
,
Also you need to convert the NavigationView
from dp to pixel before setting it as a Layout parameter.
Below is a full functional example for creating DrawerLayout
and NavigationView
programmatically
Note: I didn't create the NavigationView
layout header & menu programmatically to keep things simpler.
Behavior
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Building Main layout
LinearLayout mainLayout = new LinearLayout(this);
mainLayout.setGravity(Gravity.CENTER);
mainLayout.setOrientation(LinearLayout.HORIZONTAL);
TextView textView = new TextView(this);
textView.setText("Some text In Main Layout");
textView.setTextSize(30);
mainLayout.addView(textView);
mainLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
// Building NavigationView layout
NavigationView navView = new NavigationView(this);
DrawerLayout.LayoutParams navParams = new DrawerLayout.LayoutParams(
convertDpToPx(250), LinearLayout.LayoutParams.MATCH_PARENT);
navParams.gravity = Gravity.START;
navView.setLayoutParams(navParams);
// Navigation View header
View child = getLayoutInflater().inflate(R.layout.nav_header_layout, null);
navView.addHeaderView(child);
// Navigation View menu
navView.inflateMenu(R.menu.nav_menu);
// Building DrawerLayout
DrawerLayout drawerLayout = new DrawerLayout(this);
// adding main layout to the DrawerLayout
drawerLayout.addView(mainLayout);
// adding NavigationView to the DrawerLayout
drawerLayout.addView(navView);
// Set activity layout to the DrawerLayout
setContentView(drawerLayout);
}
/**
* Convert from dp to Pixels
*/
private int convertDpToPx(int dp) {
return Math.round(dp * (getResources().getDisplayMetrics().xdpi / DisplayMetrics.DENSITY_DEFAULT));
}
}
Layout
NavigationView Header Layout (nav_header_layout.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="180dp"
android:background="@color/colorPrimary"
android:gravity="bottom"
android:orientation="vertical"
android:paddingLeft="16dp"
android:paddingTop="16dp"
android:paddingRight="16dp"
android:paddingBottom="16dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="8dp"
app:srcCompat="@mipmap/ic_launcher_round" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:text="Header Title"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Header Subtitle" />
</LinearLayout>
NavigationView menu (nav_menu.xml)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_home"
android:icon="@drawable/ic_menu_camera"
android:title="Home" />
<item
android:id="@+id/nav_gallery"
android:icon="@drawable/ic_menu_gallery"
android:title="Gallery" />
<item
android:id="@+id/nav_slideshow"
android:icon="@drawable/ic_menu_slideshow"
android:title="Slideshow" />
</group>
</menu>
Here is a demo
Wish that help you out
来源:https://stackoverflow.com/questions/60390655/how-to-add-the-navigation-view-using-java-code