如何更改工具栏子视图的对齐方式
How do I Change the Alignment of Toolbar Child Views
我有一个工具栏,我想在 xml 中的布局中添加一个 ImageView
。我想让它右对齐,而不是默认的左对齐。
根据 documentation:
The application may add arbitrary child views to the Toolbar. They will appear at this position within the layout. If a child view's Toolbar.LayoutParams indicates a Gravity value of CENTER_HORIZONTAL the view will attempt to center within the available space remaining in the Toolbar after all other elements have been measured.
但是我无法设置重力。
我的布局
<android.support.v7.widget.Toolbar
style="@style/ToolbarStyle"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize">
<ImageView
android:id="@+id/bluetoothState"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_bluetooth_status_white"
android:contentDescription="@string/content_description_bluetooth_status"
android:padding="8dp"/>
</android.support.v7.widget.Toolbar>
android:layout_gravity="right"
就是答案。问题链接的文档甚至建议。 (好吧,它提到了重力,但没有以特定的 xml 属性方式需要)
无论如何,问题是 Android Studio 并没有真正建议自定义视图的属性。这包括 android.support.v7.widget.Toolbar
。 Design 选项卡不会将 layout_gravity 列为属性,如果您在文本选项卡上键入它也不会自动完成它。
下面是完整的示例代码..
<android.support.v7.widget.Toolbar
style="@style/ToolbarStyle"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize">
<ImageView
android:id="@+id/bluetoothState"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_bluetooth_status_white"
android:contentDescription="@string/content_description_bluetooth_status"
android:padding="8dp"
android:layout_gravity="right"/>
</android.support.v7.widget.Toolbar>
我有一个工具栏,我想在 xml 中的布局中添加一个 ImageView
。我想让它右对齐,而不是默认的左对齐。
根据 documentation:
The application may add arbitrary child views to the Toolbar. They will appear at this position within the layout. If a child view's Toolbar.LayoutParams indicates a Gravity value of CENTER_HORIZONTAL the view will attempt to center within the available space remaining in the Toolbar after all other elements have been measured.
但是我无法设置重力。
我的布局
<android.support.v7.widget.Toolbar
style="@style/ToolbarStyle"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize">
<ImageView
android:id="@+id/bluetoothState"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_bluetooth_status_white"
android:contentDescription="@string/content_description_bluetooth_status"
android:padding="8dp"/>
</android.support.v7.widget.Toolbar>
android:layout_gravity="right"
就是答案。问题链接的文档甚至建议。 (好吧,它提到了重力,但没有以特定的 xml 属性方式需要)
无论如何,问题是 Android Studio 并没有真正建议自定义视图的属性。这包括 android.support.v7.widget.Toolbar
。 Design 选项卡不会将 layout_gravity 列为属性,如果您在文本选项卡上键入它也不会自动完成它。
下面是完整的示例代码..
<android.support.v7.widget.Toolbar
style="@style/ToolbarStyle"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize">
<ImageView
android:id="@+id/bluetoothState"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_bluetooth_status_white"
android:contentDescription="@string/content_description_bluetooth_status"
android:padding="8dp"
android:layout_gravity="right"/>
</android.support.v7.widget.Toolbar>