我想在我的导航抽屉中添加微调器作为项目。我应该把旋转器放在哪里?旋转器的布局在哪里膨胀?在哪里初始化微调器?我希望它看起来像这样:
这是我添加项目的地方:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group
android:id="@+id/group1"
android:checkableBehavior="single">
<item
android:id="@+id/nav_login"
android:icon="@drawable/ic_login"
android:title="@string/login_menu_item"/>
<item
android:id="@+id/nav_signup"
android:icon="@drawable/ic_signup"
android:title="@string/signup_menu_item"/>
</group>
<item android:title="@string/language">
<menu>
<item
android:id="@+id/nav_eng"
android:title="@string/english">
</item>
<item
android:id="@+id/nav_heb"
android:title="@string/hebrew">
</item>
</menu>
这是我的抽屉布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
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:id="@+id/toolbar_home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:title="Masü"
/>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="start">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
/>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:itemIconTint="@color/colorAccent"
app:itemTextColor="@color/textColorSecondary"
app:menu="@menu/activity_home_drawer"/>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
点击一个项目,这就是它的工作原理:
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
mDrawerLayout.closeDrawers();
if (id == R.id.nav_login) {
if (mIsLoggedin) {
logout();
} else {
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.fragment_container, new LoginFragment()).commit();
}
答案 0 :(得分:10)
步骤1.请在menu.xml中添加项目
<item
android:id="@+id/navigation_drawer_item3"
android:icon="@android:drawable/ic_menu_share"
android:title=""
app:actionLayout="@layout/spinner"/>
步骤2.请为微调器视图创建布局
<?xml version="1.0" encoding="utf-8"?>
<Spinner xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:gravity="center_vertical" />
步骤3.在活动文件中设置微调器数据
Spinner spinner = (Spinner) navigationView.getMenu().findItem(R.id.navigation_drawer_item3).getActionView();
spinner.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,language));
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this,language[position],Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
步骤4.如果需要,请将android支持设计库添加到项目中。
答案 1 :(得分:3)
我将指导您如何在工具栏中创建自定义微调器,以便您可以从这里获得想法。
第1步添加支持库并创建工具栏布局
<强> toolbar.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimaryDark"
android:minHeight="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
>
<Spinner
android:id="@+id/spinner_nav"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v7.widget.Toolbar>
第2步在您的活动布局文件中包含工具栏,如下所示
<强> spintoolbaractivity.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#c9c9c9"
android:orientation="vertical" >
<include
android:id="@+id/toolbar"
layout="@layout/spintoolbar" />
</LinearLayout>
<强> spinner_row.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/spinner_selector"
android:orientation="vertical" >
<TextView
android:id="@+id/tvCategory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:textSize="18sp"
/>
</RelativeLayout>
<强>值/ styles.xml 强>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppTheme" parent="AppTheme.Base" />
<style name="AppTheme.Base" parent="Theme.AppCompat.NoActionBar">
<!-- your app branding color for the app bar -->
<item name="colorPrimary">@color/md_teal_500_primary</item>
<!-- darker variant for the status bar and contextual app bars -->
<item name="colorPrimaryDark">@color/md_teal_700</item>
<!-- theme UI controls like checkboxes and text fields -->
<item name="colorAccent">@color/md_teal_900</item>
</style>
第3步为微调器创建此自定义适配器。
<强> CustomSpinnerAdapter.java 强>
import java.util.ArrayList;
import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
// Custom Adapter for Spinner
public class CustomSpinnerAdapter extends ArrayAdapter<String> {
private Context context1;
private ArrayList<String> data;
public Resources res;
LayoutInflater inflater;
public CustomSpinnerAdapter(Context context, ArrayList<String> objects) {
super(context, R.layout.spinner_row, objects);
context1 = context;
data = objects;
inflater = (LayoutInflater) context1
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
// This funtion called for each row ( Called data.size() times )
public View getCustomView(int position, View convertView, ViewGroup parent) {
View row = inflater.inflate(R.layout.spinner_row, parent, false);
TextView tvCategory = (TextView) row.findViewById(R.id.tvCategory);
tvCategory.setText(data.get(position).toString());
return row;
}
}
<强> SpinToolbarActivity.java 强>
import java.util.ArrayList;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;
import android.widget.Toast;
public class SpinToolbarActivity extends AppCompatActivity {
private Toolbar toolbar;
private Spinner spinner_nav;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spintoolbaractivity);
toolbar = (Toolbar) findViewById(R.id.toolbar);
spinner_nav = (Spinner) findViewById(R.id.spinner_nav);
if (toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
addItemsToSpinner();
}
// add items into spinner dynamically
public void addItemsToSpinner() {
ArrayList<String> list = new ArrayList<String>();
list.add("Top News");
list.add("Politics");
list.add("Business");
list.add("Sports");
list.add("Movies");
// Custom ArrayAdapter with spinner item layout to set popup background
CustomSpinnerAdapter spinAdapter = new CustomSpinnerAdapter(
getApplicationContext(), list);
// Default ArrayAdapter with default spinner item layout, getting some
// view rendering problem in lollypop device, need to test in other
// devices
/*
* ArrayAdapter<String> spinAdapter = new ArrayAdapter<String>(this,
* android.R.layout.simple_spinner_item, list);
* spinAdapter.setDropDownViewResource
* (android.R.layout.simple_spinner_dropdown_item);
*/
spinner_nav.setAdapter(spinAdapter);
spinner_nav.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapter, View v,
int position, long id) {
// On selecting a spinner item
String item = adapter.getItemAtPosition(position).toString();
// Showing selected spinner item
Toast.makeText(getApplicationContext(), "Selected : " + item,
Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
Toast.makeText(getApplicationContext(), "Settings Clicked",
Toast.LENGTH_SHORT).show();
return true;
} else if (id == R.id.action_search) {
Toast.makeText(getApplicationContext(), "Search Clicked",
Toast.LENGTH_SHORT).show();
return true;
} else if (id == R.id.action_add) {
Toast.makeText(getApplicationContext(), "Add Clicked",
Toast.LENGTH_SHORT).show();
return true;
} else if (id == R.id.action_delete) {
Toast.makeText(getApplicationContext(), "Delete Clicked",
Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
}
看到你的微调器看起来像这样