问题描述
我正在使用androidx进行开发,并且尝试使用NavigationView
,并且由于findViewById
通常在您需要时不起作用,因此我尝试使用Java代码进行所有操作.所以,我试图让这个NavigationView
出现,但这似乎缺少了一些东西:
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);
}
似乎出现了Drawer
,LinearLayout
和TextView
,但是当我从屏幕的右边缘向左滑动时,使NavigationView
出现.我们应该如何添加和实例化Views(在本例中为NavigationView
)?
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)?
推荐答案
您几乎在正确的路径上,需要将mNavView重力更改为Gravity.START,并对NavigationView
使用DrawerLayout.LayoutParams
,
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
,
此外,您还需要将NavigationView
从dp转换为像素,然后再将其设置为Layout参数.
Also you need to convert the NavigationView
from dp to pixel before setting it as a Layout parameter.
以下是用于以编程方式创建DrawerLayout
和NavigationView
的完整功能示例
Below is a full functional example for creating DrawerLayout
and NavigationView
programmatically
注意:我没有创建NavigationView
布局标题&通过编程菜单使事情变得更简单.
Note: I didn't create the NavigationView
layout header & menu programmatically to keep things simpler.
行为
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));
}
}
布局
NavigationView标头布局(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菜单(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>
这是一个演示
希望对您有帮助
这篇关于如何使用Java代码添加导航视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!