在工具栏或操作栏的中心设置 ActionBarDrawerToggle,android?
Set ActionBarDrawerToggle in center of toolbar or actionbar, android?
我已经通过 java 创建了我的 activity XML 并且还放置了导航抽屉。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDrawerLayout = new DrawerLayout(getApplicationContext());
mDrawerLayout.setLayoutParams(new DrawerLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
content_frame = new FrameLayout(getApplicationContext());
content_frame.setLayoutParams(new FrameLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
content_layout = new LinearLayout(getApplicationContext());
content_layout.setOrientation(LinearLayout.VERTICAL);
content_layout.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
content_layout.setBackgroundColor(Color.parseColor("#FFFFFF"));;
tool = new Toolbar(getApplicationContext());
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
tool.setLayoutParams(lp);
tool.setBackgroundColor(Color.parseColor("#00BBD3"));
setSupportActionBar(tool);
tool.setMinimumHeight(getSupportActionBar().getHeight());
content_layout1 = new LinearLayout(getApplicationContext());
content_layout1.setOrientation(LinearLayout.VERTICAL);
content_layout1.setLayoutParams(new DrawerLayout.LayoutParams(320,
LayoutParams.MATCH_PARENT, Gravity.START));
t = new TextView(getApplicationContext());
t.setTextColor(Color.BLACK);
t.setText("XYZ");
ivBottom = new ImageView(getApplicationContext());
LinearLayout.LayoutParams par = new LinearLayout.LayoutParams(50, 50);
par.gravity = Gravity.CENTER_HORIZONTAL;
ivBottom.setLayoutParams(par);
ivBottom.setBackgroundResource(R.drawable.ic_launcher);
mDrawerList = new ListView(getApplicationContext());
mDrawerList.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
content_layout1.setBackgroundColor(Color.BLACK);
content_layout.addView(tool);
content_layout.addView(t);
content_frame.addView(content_layout);
mDrawerLayout.addView(content_frame);
content_layout1.addView(mDrawerList);
content_layout1.addView(ivBottom);
mDrawerLayout.addView(content_layout1);
setContentView(mDrawerLayout);
// Set the adapter for the list view
mDrawerList.setAdapter(new SideAdapter(getApplicationContext(),
menuTitles, menuIcons));
// Set the list's click listener
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, tool,
R.string.action_settings, R.string.action_settings) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
};
// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
如果我用工具栏高度来包裹内容,这就是我得到的 toolbar/action 栏
如您所见,我没有获得操作栏的首选高度。
现在,如果我将高度保持在 100,这就是我得到的 toolbar/action 栏
如您所见,操作栏切换图标未与内容居中
我有两个选择:
- 要么工具栏的大小达到默认大小
- 或者让开关居中
我已经尝试了很多东西,但我还是想不通。
请帮帮我。
菜单 XML
<menu 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"
tools:context="com.example.drawermenu.JavaDrawerActivity" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never"/>
Java 菜单代码
@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 onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content
// view
// boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
// menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
@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) {
return true;
}
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
the size of toolbar comes to default size
如果您指的是默认的Action-Bar高度并且您使用的是内置主题,属性"android:actionBarSize"值就是当前主题中的Action-Bar高度。如果你想在代码中设置高度,代码片段如下。
TypedValue tv = new TypedValue();
if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
int actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
}
make toggle be in center
据我所知,没有正式的方法来更改 "toggle drawable" 的布局参数,但您可以使用以下代码(在 android 5.0 nexus- 上测试) 5、使用support-v7 Drawabletoggle and Toolbar):
for (int i = 0; i < mToolbar.getChildCount(); i++) {
// make toggle drawable center-vertical, you can make each view alignment whatever you want
if (tool.getChildAt(i) instanceof ImageButton) {
Toolbar.LayoutParams lp = (Toolbar.LayoutParams) mToolbar.getChildAt(i).getLayoutParams();
lp.gravity = Gravity.CENTER_VERTICAL;
}
}
所以,你可以看到它只是一种设置view layout-gravity的常用方法,需要知道的是toggle drawable是一个ImageButton。如果想让Toobar中的每个view都居中垂直,只需要设置相应的view的layout-gravity即可。玩得开心! :)
另一种更简单的方法是在工具栏上设置最小高度,因为默认情况下它使用操作栏的大小
android:minHeight="?android:attr/actionBarSize"
因此在您的工具栏中,如果您将其设置为 100dp
<android.support.v7.widget.Toolbar
android:minHeight="100dp"
它将与工具栏中间对齐。
我已经通过 java 创建了我的 activity XML 并且还放置了导航抽屉。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDrawerLayout = new DrawerLayout(getApplicationContext());
mDrawerLayout.setLayoutParams(new DrawerLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
content_frame = new FrameLayout(getApplicationContext());
content_frame.setLayoutParams(new FrameLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
content_layout = new LinearLayout(getApplicationContext());
content_layout.setOrientation(LinearLayout.VERTICAL);
content_layout.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
content_layout.setBackgroundColor(Color.parseColor("#FFFFFF"));;
tool = new Toolbar(getApplicationContext());
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
tool.setLayoutParams(lp);
tool.setBackgroundColor(Color.parseColor("#00BBD3"));
setSupportActionBar(tool);
tool.setMinimumHeight(getSupportActionBar().getHeight());
content_layout1 = new LinearLayout(getApplicationContext());
content_layout1.setOrientation(LinearLayout.VERTICAL);
content_layout1.setLayoutParams(new DrawerLayout.LayoutParams(320,
LayoutParams.MATCH_PARENT, Gravity.START));
t = new TextView(getApplicationContext());
t.setTextColor(Color.BLACK);
t.setText("XYZ");
ivBottom = new ImageView(getApplicationContext());
LinearLayout.LayoutParams par = new LinearLayout.LayoutParams(50, 50);
par.gravity = Gravity.CENTER_HORIZONTAL;
ivBottom.setLayoutParams(par);
ivBottom.setBackgroundResource(R.drawable.ic_launcher);
mDrawerList = new ListView(getApplicationContext());
mDrawerList.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
content_layout1.setBackgroundColor(Color.BLACK);
content_layout.addView(tool);
content_layout.addView(t);
content_frame.addView(content_layout);
mDrawerLayout.addView(content_frame);
content_layout1.addView(mDrawerList);
content_layout1.addView(ivBottom);
mDrawerLayout.addView(content_layout1);
setContentView(mDrawerLayout);
// Set the adapter for the list view
mDrawerList.setAdapter(new SideAdapter(getApplicationContext(),
menuTitles, menuIcons));
// Set the list's click listener
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, tool,
R.string.action_settings, R.string.action_settings) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
};
// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
如果我用工具栏高度来包裹内容,这就是我得到的 toolbar/action 栏
如您所见,我没有获得操作栏的首选高度。
现在,如果我将高度保持在 100,这就是我得到的 toolbar/action 栏 如您所见,操作栏切换图标未与内容居中
我有两个选择:
- 要么工具栏的大小达到默认大小
- 或者让开关居中
我已经尝试了很多东西,但我还是想不通。 请帮帮我。
菜单 XML
<menu 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"
tools:context="com.example.drawermenu.JavaDrawerActivity" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never"/>
Java 菜单代码
@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 onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content
// view
// boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
// menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
@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) {
return true;
}
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
the size of toolbar comes to default size
如果您指的是默认的Action-Bar高度并且您使用的是内置主题,属性"android:actionBarSize"值就是当前主题中的Action-Bar高度。如果你想在代码中设置高度,代码片段如下。
TypedValue tv = new TypedValue();
if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
int actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
}
make toggle be in center
据我所知,没有正式的方法来更改 "toggle drawable" 的布局参数,但您可以使用以下代码(在 android 5.0 nexus- 上测试) 5、使用support-v7 Drawabletoggle and Toolbar):
for (int i = 0; i < mToolbar.getChildCount(); i++) {
// make toggle drawable center-vertical, you can make each view alignment whatever you want
if (tool.getChildAt(i) instanceof ImageButton) {
Toolbar.LayoutParams lp = (Toolbar.LayoutParams) mToolbar.getChildAt(i).getLayoutParams();
lp.gravity = Gravity.CENTER_VERTICAL;
}
}
所以,你可以看到它只是一种设置view layout-gravity的常用方法,需要知道的是toggle drawable是一个ImageButton。如果想让Toobar中的每个view都居中垂直,只需要设置相应的view的layout-gravity即可。玩得开心! :)
另一种更简单的方法是在工具栏上设置最小高度,因为默认情况下它使用操作栏的大小
android:minHeight="?android:attr/actionBarSize"
因此在您的工具栏中,如果您将其设置为 100dp
<android.support.v7.widget.Toolbar
android:minHeight="100dp"
它将与工具栏中间对齐。