Android: 自定义操作栏,如何使用整个宽度?
Android: Custom action bar,How to use entire width?
这是我的自定义操作栏的图片:
我想使用整个宽度,我在互联网上搜索了解决方案,但没有找到适合我的方法。
这是我的操作栏。xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<ImageView
android:layout_width="50dp"
android:layout_height="match_parent"
android:id="@+id/invActionBarSave"
android:src="@drawable/ic_ok_b"
android:nestedScrollingEnabled="true"
android:layout_alignParentRight="true"
android:background="#ffb0afaf"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp" />
<ImageView
android:layout_width="50dp"
android:layout_height="match_parent"
android:id="@+id/invActionBarDelete"
android:src="@drawable/ic_trash"
android:nestedScrollingEnabled="true"
android:layout_toLeftOf="@+id/invActionBarSave"
android:layout_marginRight="5dp"
android:background="#ffb0afaf"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:singleLine="true"
android:text="אלסאדק אחמד אבו האני"
android:textSize="18dp"
android:textColor="#ff000000"
android:id="@+id/invActionBarTitle"
android:gravity="center|right"
android:layout_toLeftOf="@+id/invActionBarDelete"
android:layout_toRightOf="@+id/invActionBarHome"
android:layout_marginRight="20dp"
android:layout_marginLeft="10dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/invActionBarHome"
android:src="@drawable/ic_back_b"
android:nestedScrollingEnabled="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="false"
android:paddingLeft="10dp" />
<ProgressBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminateOnly="false"
style="@style/ProgressBar.Horizontal"
android:indeterminate="true"
android:id="@+id/invActionBarProgressBar"
android:visibility="gone"
android:layout_below="@+id/invActionBarDelete"
android:paddingLeft="10dp" />
</RelativeLayout>
这是我的 java 代码:
getSupportActionBar().setDisplayShowHomeEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(false);
LayoutInflater mInflater = LayoutInflater.from(this);
//View mCustomView = mInflater.inflate(R.layout.actionbar_invoice, null);
getSupportActionBar().setCustomView(R.layout.actionbar_invoice);
getSupportActionBar().setDisplayShowCustomEnabled(true);
setContentView(R.layout.cstmrinvoice);
getWindow().
getDecorView().
setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
[编辑]
这是一个简单的:
这里是 .xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:contentInsetEnd="0dp"
android:contentInsetStart="0dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="@+id/textView6"
android:background="#ffff0000"
android:textColor="#ffffffff" />
</RelativeLayout>
同样的结果,操作栏右边还有一个space!
谢谢
我通过使用工具栏并向其添加以下属性解决了这个问题。
<FrameLayout 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="wrap_content" >
<View
android:id="@+id/toolbar_blurrbg"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@drawable/toolbar_bg"
android:visibility="gone" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@drawable/actionbar_bg"
app:contentInsetEnd="0dp"
app:contentInsetStart="0dp" />
app:contentInsetEnd 和 app:contentInsetStart ..
看看对你有没有帮助
阅读此 link1, link2 并将 contentInsets 设置为 0dp,如下所示:
View mCustomView = mInflater.inflate(R.layout.actionbar_invoice, null);
getSupportActionBar().setCustomView(mCustomView);
Toolbar toolbar=(Toolbar)mCustomView.getParent();
toolbar.setContentInsetsAbsolute(0,0);
如果您使用自定义工具栏而不是操作栏,请使用以下代码:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="@dimen/action_bar_height"
android:background="?attr/colorPrimary"
android:contentInsetStart="0dp"//see this
android:contentInsetLeft="0dp"//see this
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
希望对您有所帮助。
遵循几个简单的步骤。
1.制作你自己的自定义动作视图
action_view.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:orientation="horizontal"
android:weightSum="3">
<TextView
android:id="@+id/bar_title1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="@color/White"
android:text="title1"/>
<TextView
android:id="@+id/bar_title2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="@color/White"
android:text="title2"/>
<TextView
android:id="@+id/bar_title3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="@color/White"
android:text="title3"/>
</LinearLayout>
代码如下:
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.custom_action_bar, null);
actionBar.setCustomView(view, new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
这一定能解决您的问题。
快乐编码!!!
ActionBar action = getSupportActionBar();
action.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
Toolbar toolbar=(Toolbar)action.getCustomView().getParent();
toolbar.setContentInsetsAbsolute(0, 0);
toolbar.getContentInsetEnd();
toolbar.setPadding(0, 0, 0, 0);
我只是想告诉大家,在某种程度上,下面的代码是行不通的。在我的情况下,屏幕边缘和工具栏之间的 space 仍然存在。
app:contentInsetEnd="0dp"
app:contentInsetStart="0dp"
我花了大约 2 个小时对样式和这两个属性进行了一些操作。什么都帮不了我。突然我想到这个 "space" 可能只是工具栏的填充或边距。但是我的工具栏没有任何填充或边距。我在布局 .xml 文件中向 Toolbar
添加了 0dp
填充。
android:padding="0dp"
成功了。你可以想象我的脸。这个 space 已经消失了。 Android 骗我的。
这是我的自定义操作栏的图片:
我想使用整个宽度,我在互联网上搜索了解决方案,但没有找到适合我的方法。
这是我的操作栏。xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<ImageView
android:layout_width="50dp"
android:layout_height="match_parent"
android:id="@+id/invActionBarSave"
android:src="@drawable/ic_ok_b"
android:nestedScrollingEnabled="true"
android:layout_alignParentRight="true"
android:background="#ffb0afaf"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp" />
<ImageView
android:layout_width="50dp"
android:layout_height="match_parent"
android:id="@+id/invActionBarDelete"
android:src="@drawable/ic_trash"
android:nestedScrollingEnabled="true"
android:layout_toLeftOf="@+id/invActionBarSave"
android:layout_marginRight="5dp"
android:background="#ffb0afaf"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:singleLine="true"
android:text="אלסאדק אחמד אבו האני"
android:textSize="18dp"
android:textColor="#ff000000"
android:id="@+id/invActionBarTitle"
android:gravity="center|right"
android:layout_toLeftOf="@+id/invActionBarDelete"
android:layout_toRightOf="@+id/invActionBarHome"
android:layout_marginRight="20dp"
android:layout_marginLeft="10dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/invActionBarHome"
android:src="@drawable/ic_back_b"
android:nestedScrollingEnabled="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="false"
android:paddingLeft="10dp" />
<ProgressBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminateOnly="false"
style="@style/ProgressBar.Horizontal"
android:indeterminate="true"
android:id="@+id/invActionBarProgressBar"
android:visibility="gone"
android:layout_below="@+id/invActionBarDelete"
android:paddingLeft="10dp" />
</RelativeLayout>
这是我的 java 代码:
getSupportActionBar().setDisplayShowHomeEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(false);
LayoutInflater mInflater = LayoutInflater.from(this);
//View mCustomView = mInflater.inflate(R.layout.actionbar_invoice, null);
getSupportActionBar().setCustomView(R.layout.actionbar_invoice);
getSupportActionBar().setDisplayShowCustomEnabled(true);
setContentView(R.layout.cstmrinvoice);
getWindow().
getDecorView().
setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
[编辑]
这是一个简单的:
这里是 .xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:contentInsetEnd="0dp"
android:contentInsetStart="0dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="@+id/textView6"
android:background="#ffff0000"
android:textColor="#ffffffff" />
</RelativeLayout>
同样的结果,操作栏右边还有一个space!
谢谢
我通过使用工具栏并向其添加以下属性解决了这个问题。
<FrameLayout 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="wrap_content" >
<View
android:id="@+id/toolbar_blurrbg"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@drawable/toolbar_bg"
android:visibility="gone" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@drawable/actionbar_bg"
app:contentInsetEnd="0dp"
app:contentInsetStart="0dp" />
app:contentInsetEnd 和 app:contentInsetStart ..
看看对你有没有帮助
阅读此 link1, link2 并将 contentInsets 设置为 0dp,如下所示:
View mCustomView = mInflater.inflate(R.layout.actionbar_invoice, null);
getSupportActionBar().setCustomView(mCustomView);
Toolbar toolbar=(Toolbar)mCustomView.getParent();
toolbar.setContentInsetsAbsolute(0,0);
如果您使用自定义工具栏而不是操作栏,请使用以下代码:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="@dimen/action_bar_height"
android:background="?attr/colorPrimary"
android:contentInsetStart="0dp"//see this
android:contentInsetLeft="0dp"//see this
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
希望对您有所帮助。
遵循几个简单的步骤。 1.制作你自己的自定义动作视图 action_view.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:orientation="horizontal"
android:weightSum="3">
<TextView
android:id="@+id/bar_title1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="@color/White"
android:text="title1"/>
<TextView
android:id="@+id/bar_title2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="@color/White"
android:text="title2"/>
<TextView
android:id="@+id/bar_title3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="@color/White"
android:text="title3"/>
</LinearLayout>
代码如下:
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.custom_action_bar, null);
actionBar.setCustomView(view, new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
这一定能解决您的问题。 快乐编码!!!
ActionBar action = getSupportActionBar();
action.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
Toolbar toolbar=(Toolbar)action.getCustomView().getParent();
toolbar.setContentInsetsAbsolute(0, 0);
toolbar.getContentInsetEnd();
toolbar.setPadding(0, 0, 0, 0);
我只是想告诉大家,在某种程度上,下面的代码是行不通的。在我的情况下,屏幕边缘和工具栏之间的 space 仍然存在。
app:contentInsetEnd="0dp"
app:contentInsetStart="0dp"
我花了大约 2 个小时对样式和这两个属性进行了一些操作。什么都帮不了我。突然我想到这个 "space" 可能只是工具栏的填充或边距。但是我的工具栏没有任何填充或边距。我在布局 .xml 文件中向 Toolbar
添加了 0dp
填充。
android:padding="0dp"
成功了。你可以想象我的脸。这个 space 已经消失了。 Android 骗我的。