这是我的代码 -
MainActivity.java
public class MainActivity extends AppCompatActivity
{
DrawerLayout mDrawerLayout;
NavigationView mNavigationView;
FragmentManager mFragmentManager;
FragmentTransaction mFragmentTransaction;
ProgressDialog pd;
JSONArray fetchedResponse;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mNavigationView = (NavigationView) findViewById(R.id.somestuff);
new GetDataFromService().execute(getString(R.string.api_url);
mNavigationView.setNavigationItemSelectedListener(new
NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
mDrawerLayout.closeDrawers();
//Do rest of the stuff
}
});
}
public class GetDataFromService extends AsyncTask<String, Void, String> {
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
pd.dismiss();
try {
fetchedResponse = new JSONArray(s);
if (fetchedResponse != null) {
final Menu menu = mNavigationView.getMenu();
for (int i = 0; i < fetchedResponse.length(); i++) {
String menu_title = fetchedResponse.getJSONObject(i).getString("menu_title");
int menu_id = fetchedResponse.getJSONObject(i).getInt("menu_id");
menu.add(Menu.NONE, menu_id, Menu.NONE, menu_title);
}
}
else{
//do error related stuff
}
}catch (JSONException e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
String exceptionAsString = sw.toString();
System.out.println("Unexpected Error :");
System.out.println(exceptionAsString);
}
}
}
activity_main.xml中
<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="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/orange"
android:id="@+id/toolbar"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:title="@string/app_name" />
<!--<include layout="@layout/toolbar"/>-->
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/drawerLayout"
>
<FrameLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/containerView">
</FrameLayout>
<android.support.design.widget.NavigationView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:id="@+id/somestuff"
app:itemTextColor="@color/black"
app:menu="@menu/drawermenu"
android:layout_marginTop="-24dp"
android:theme="@style/MyNavigationView"
/>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
drawermenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
</menu>
styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="windowActionBar">false</item>
<!-- colorPrimary is used for the default action bar background -->
<item name="windowActionModeOverlay">true</item>
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/orange</item>
<item name="colorPrimaryDark">@android:color/holo_orange_dark</item>
</style>
我能够填充MenuItems,但我不知道如何在每个项目之间添加分隔符(因为它们是动态的)以及如何在选择时突出显示每个项目?
答案 0 :(得分:1)
2天前解决了,但很抱歉发布答案太晚了。 希望解决方案能帮到某人。 要突出显示抽屉菜单项,请在动态创建菜单时启用菜单的组可检查属性。
因此,在创建菜单项的MainActivity.java中,只需添加以下代码
即可//Enable the group checkable property
menu.setGroupCheckable(<any integer representing group id>,true,true);
//Then highlight the default menu item
menu.getItem(0).setChecked(true);
然后在“android.support.design.widget.NavigationView”标签内的activity_main.xml中添加以下行 -
android:theme="@style/MyNavigationView"
最后,在styles.xml中,在样式标记中创建上述主题,如下所示 -
<style name="MyNavigationView" parent="Widget.Design.NavigationView">
<item name="android:dividerHeight">1dp</item>
<item name="android:layout_marginTop">10dp</item>
<item name="colorControlHighlight">#3dfdfd</item>
</style>
其次,事实证明,我对它的外观感到满意,因此在任何给定时间都不需要分隔符,至少选择了一个项目。
希望这有助于某人:)