【问题标题】:Toolbar content looks different on preview mode than emulator预览模式下的工具栏内容看起来与模拟器不同
【发布时间】:2017-02-13 08:27:31
【问题描述】:

我正在尝试在我的应用程序中实现自定义工具栏。我想在左边有 1 个图像和 1 个文本视图,中间有 1 个文本视图,最后有 1 个文本视图。在预览模式下,它看起来像预期的那样: 但是,当我在虚拟设备上启动我的应用程序时,所有视图都与开头对齐:

我的代码:

<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 android-layout android-custom-view android-toolbar


    【解决方案1】:

    用下面的代码替换你的代码,这可能对你有帮助

     <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>
    

    【讨论】:

      【解决方案2】:

      问题已解决:在其他 xml 文件中,我需要将 layout_width 从 wrap_content 更改为 match_parent...感谢您尝试帮助我...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-10-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-26
        • 2023-04-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多