Android 工具栏内容在预览模式下与emulator上看起来不同
我正在尝试在我的应用程序中实现自定义工具栏。我想在左边有1个图像和1个文本视图,中间有1个TealVIEW,最后有1个TealVIEW。在预览模式下,它看起来与预期一致: 但是,当我在虚拟设备上启动应用程序时,所有视图都与开始对齐: 我的代码:Android 工具栏内容在预览模式下与emulator上看起来不同,android,android-layout,android-custom-view,android-toolbar,Android,Android Layout,Android Custom View,Android Toolbar,我正在尝试在我的应用程序中实现自定义工具栏。我想在左边有1个图像和1个文本视图,中间有1个TealVIEW,最后有1个TealVIEW。在预览模式下,它看起来与预期一致: 但是,当我在虚拟设备上启动应用程序时,所有视图都与开始对齐: 我的代码: <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="ht
<android.support.v7.widget.Toolbar
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"
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
tools:context=".OptionListActivity">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:weightSum="3">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@android:drawable/btn_star_big_on"
android:id="@+id/imageView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/nameSurname"
android:text="June Office"
android:textColor="@android:color/white"
android:textAlignment="viewStart"
android:layout_weight="1"
android:layout_marginStart="5dp" />
<TextView
android:text="Dec 19, 12:47 PM"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/dateTime"
android:textColor="@android:color/white"
android:textAlignment="center"
android:layout_weight="1"/>
<TextView
android:text="415-555-1212"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/number"
android:textColor="@android:color/white"
android:textAlignment="viewEnd"
android:layout_weight="1"/>
</LinearLayout>
</android.support.v7.widget.Toolbar>
我需要改变什么才能得到预期的结果?也许有人可以解释为什么它在预览模式下看起来不错,而在模拟器上看起来不正确…用下面的代码替换您的代码这可能会对您有所帮助
<android.support.v7.widget.Toolbar
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"
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
tools:context=".OptionListActivity">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:weightSum="3">
<LinearLayout
android:orientation="horizontal"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=1>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@android:drawable/btn_star_big_on"
android:id="@+id/imageView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/nameSurname"
android:text="June Office"
android:textColor="@android:color/white"
android:textAlignment="viewStart"
android:layout_marginStart="5dp" />
</LinearLayout>
<TextView
android:text="Dec 19, 12:47 PM"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/dateTime"
android:textColor="@android:color/white"
android:textAlignment="center"
android:layout_weight="1"/>
<TextView
android:text="415-555-1212"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/number"
android:textColor="@android:color/white"
android:textAlignment="viewEnd"
android:layout_weight="1"/>
</LinearLayout>
</android.support.v7.widget.Toolbar>
问题已解决:在另一个xml文件中,我需要将布局宽度从包裹内容更改为匹配父项。。。谢谢你试着帮助我