Android: пользовательская панель действий, как использовать всю ширину?
Вот изображение моей настраиваемой панели действий:
![enter image description here]()
Я хочу использовать всю ширину, я искал интернет для решения, но ничего не было для меня.
Вот моя панель действий .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>
Ответы
Ответ 1
Прочтите этот 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);
Если вы используете пользовательскую панель инструментов вместо ActionBar, используйте ниже код:
<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">
Надеюсь, это поможет вам.
Ответ 2
Следуйте нескольким простым шагам.
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);
Это, несомненно, решит вашу цель.
Счастливое кодирование!!!
Ответ 3
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);
Ответ 4
Я решил это, используя панель инструментов и добавив к ней следующие свойства.
<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 и приложение: contentInsetStart..
Посмотрите, поможет ли вам