将ActionBarDrawerToggle设置在工具栏或actionbar的中心,android?
我已经通过java创建了我的活动XML,并将导航抽屉放在其中将ActionBarDrawerToggle设置在工具栏或actionbar的中心,android?,android,toolbar,android-toolbar,actionbardrawertoggle,Android,Toolbar,Android Toolbar,Actionbardrawertoggle,我已经通过java创建了我的活动XML,并将导航抽屉放在其中 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mDrawerLayout = new DrawerLayout(getApplicationContext()); mDrawerLayout.setLayoutParams(new DrawerLayout.LayoutParams
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);
}
如果我将工具栏高度设置为包裹内容,这就是我得到的工具栏/操作栏
正如你所见,我没有得到行动栏的首选高度。
现在,如果我将高度保持在100,这就是我得到的工具栏/操作栏
正如您所看到的,actionbar切换图标不以内容为中心
我有两个选择:
<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"/>
工具栏的大小为默认大小
如果您表示默认的操作栏高度,并且使用内置主题,则属性“android:actionBarSize”值正好是当前主题中的操作栏高度。如果要在代码中设置高度,代码段如下所示
TypedValue tv = new TypedValue();
if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
int actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
}
使钮扣在中间
据我所知,目前还没有官方的方法来更改“toggle drawable”的布局参数,但您可以使用以下代码(在android 5.0 nexus-5上测试,使用support-v7 Drawabletoggle和Toolbar):
for(int i=0;i
因此,您可以看到,这只是设置视图布局重力的一种常见方法,需要知道的是切换可绘制是一个图像按钮。如果希望Toobar中的每个视图都垂直居中,只需设置视图的相应布局重力。玩得开心!:)
工具栏的大小为默认大小
如果您表示默认的操作栏高度,并且使用内置主题,则属性“android:actionBarSize”值正好是当前主题中的操作栏高度。如果要在代码中设置高度,代码段如下所示
TypedValue tv = new TypedValue();
if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
int actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
}
使钮扣在中间
据我所知,目前还没有官方的方法来更改“toggle drawable”的布局参数,但您可以使用以下代码(在android 5.0 nexus-5上测试,使用support-v7 Drawabletoggle和Toolbar):
for(int i=0;i
因此,您可以看到,这只是设置视图布局重力的一种常见方法,需要知道的是切换可绘制是一个图像按钮。如果希望Toobar中的每个视图都垂直居中,只需设置视图的相应布局重力。玩得开心!:) 另一个更简单的方法是在工具栏上设置最小高度,因为默认情况下它使用动作栏的大小
android:minHeight="?android:attr/actionBarSize"
因此,如果您将工具栏设置为100dp
<android.support.v7.widget.Toolbar
android:minHeight="100dp"
另一种更简单的方法是在工具栏上设置最小高度,因为默认情况下它使用动作栏的大小
android:minHeight="?android:attr/actionBarSize"
因此,如果您将工具栏设置为100dp
<android.support.v7.widget.Toolbar
android:minHeight="100dp"
<代码>你能改变重力吗?重力=重力;中心垂直;你能改变重力吗?重力=重力;中心垂直;在“我的当前代码设置”菜单中未显示。你也能帮忙吗?@Shink你是说“动作按钮”吗?如果是,请显示相应的xml文件?尝试将app:showAsAction=“never”
替换为android:showAsAction=“never”
@Shink抱歉,我尝试过了,它在nexus5(5.0)中工作,你知道为什么吗。。。顺便说一句,新的ActionBarDrawerToggle(这个,mDrawerLayout,工具,R.string.action\u设置,R.string.action\u设置)
应该被替换为另一个构造函数,由官方文档指示。如果您不想将工具栏高度设置为?actionBarSize,这是唯一的解决方案,谢谢!!在“我的当前代码设置”菜单中未显示。你也能帮忙吗?@Shink你是说“动作按钮”吗?如果是,请显示相应的xml文件?尝试将app:showAsAction=“never”
替换为android:showAsAction=“never”
@Shink抱歉,我尝试过了,它在nexus5(5.0)中工作,你知道为什么吗。。。顺便说一句,新的ActionBarDrawerToggle(这个,mDrawerLayout,工具,R.string.action\u设置,R.string.action\u设置)
应该被替换为另一个构造函数,由官方文档指示。如果您不想将工具栏高度设置为?actionBarSize,这是唯一的解决方案,谢谢!!哇,这是一种更好的方法,更整洁!谢谢,谢谢你,伙计。回答得很好。又甜又短:)哇,这是一个更好的方法,更整洁!谢谢,谢谢你,伙计。回答得很好。又甜又短:)