如何使文本居中并将可绘制对象左对齐?
How to center text and align drawable to the left?
有什么办法可以让文本视图中的文本居中,然后让可绘制对象与左侧对齐,以便文本保持居中但可绘制对象不会影响这一点?目前整个textview都居中。
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:elevation="4dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MirrorMirror"
android:textStyle="bold"
android:drawableLeft="@drawable/ic_mirror_face"
android:textAlignment="center"
android:textSize="20sp"
android:textColor="@android:color/white"
android:id="@+id/toolbar_title"
android:layout_gravity="center"/>
</android.support.v7.widget.Toolbar>
您可以在这张图片中看到问题。我想让这个词以图书馆这个词为中心。
我在一些应用程序中尝试了这个技巧,根据可绘制宽度在减号 (-) 中设置 textview 的左边距,也可能对您有所帮助。你可以试试这样添加
android:layout_marginLeft="-10dp"
我知道这不是解决您的问题的确切方法,但有时它会有所帮助。
我已经完成了我的其中一个项目
你可以看看 here
<LinearLayout
android:id="@+id/ll_action"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentTop="true">
<TextView
android:id="@+id/btn_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
android:drawableLeft="@drawable/ic_done_black_24dp"
android:drawableStart="@drawable/ic_done_black_24dp"
android:layout_weight="1"
android:layout_gravity="center"
android:gravity="center"
android:background="@color/colorButtonBg"
android:textColor="@color/colorInputTextAccent"
/>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@color/colorAlphaBlack"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
/>
<TextView
android:id="@+id/btn_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:drawableLeft="@drawable/ic_clear_black_24dp"
android:drawableStart="@drawable/ic_clear_black_24dp"
android:layout_weight="1"
android:gravity="center"
android:background="@color/colorButtonBg"
android:textColor="@color/colorInputTextAccent"
/>
</LinearLayout>
ic_done_black_24dp.xml 可绘制
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M9,16.2L4.8,12l-1.4,1.4L9,19 21,7l-1.4,-1.4L9,16.2z"/>
我看到这里发布了一个答案,此后已被删除。该解决方案应该适合您。
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MirrorMirror"
android:textStyle="bold"
android:drawableLeft="@mipmap/ic_launcher"
android:textAlignment="center"
android:textSize="20sp"
android:id="@+id/toolbar_title"
android:layout_gravity="center"
android:layout_marginRight="10dp"/>
最后一行是应该做的事情。将边距值从 10dp
更改为最适合您正在使用的图像的值。
或
您可以使用锚定到 TextView
的 ImageView
来获得相同的结果。但在这种情况下您将拥有更多控制权,因为可绘制对象的大小不会影响文本的对齐方式。
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="someText"
android:layout_gravity="center"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
app:layout_anchor="@id/toolbar_title"
android:layout_gravity="start|center"
app:layout_anchorGravity="start|center"/>
</android.support.design.widget.CoordinatorLayout>
这就是输出的样子:
编辑
我现在意识到您面临的问题可能是由于使用 ToolBar
时出现的偏移量 (?)。 This 答案为此提供了一个很好的解决方案。它是这样的
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
</android.support.v7.widget.Toolbar>
<TextView
android:id="@+id/toolbar_title"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MirrorMirror"
android:textStyle="bold"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:layout_toLeftOf="@id/toolbar_title"/>
</RelativeLayout>
在这里我不是将TextView
和ImageView
放在Toolbar
里面因为它会再次稍微向右偏移。相反,我将它放在 上面 Toolbar
有什么办法可以让文本视图中的文本居中,然后让可绘制对象与左侧对齐,以便文本保持居中但可绘制对象不会影响这一点?目前整个textview都居中。
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:elevation="4dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MirrorMirror"
android:textStyle="bold"
android:drawableLeft="@drawable/ic_mirror_face"
android:textAlignment="center"
android:textSize="20sp"
android:textColor="@android:color/white"
android:id="@+id/toolbar_title"
android:layout_gravity="center"/>
</android.support.v7.widget.Toolbar>
您可以在这张图片中看到问题。我想让这个词以图书馆这个词为中心。
我在一些应用程序中尝试了这个技巧,根据可绘制宽度在减号 (-) 中设置 textview 的左边距,也可能对您有所帮助。你可以试试这样添加
android:layout_marginLeft="-10dp"
我知道这不是解决您的问题的确切方法,但有时它会有所帮助。
我已经完成了我的其中一个项目 你可以看看 here
<LinearLayout
android:id="@+id/ll_action"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentTop="true">
<TextView
android:id="@+id/btn_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
android:drawableLeft="@drawable/ic_done_black_24dp"
android:drawableStart="@drawable/ic_done_black_24dp"
android:layout_weight="1"
android:layout_gravity="center"
android:gravity="center"
android:background="@color/colorButtonBg"
android:textColor="@color/colorInputTextAccent"
/>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@color/colorAlphaBlack"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
/>
<TextView
android:id="@+id/btn_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:drawableLeft="@drawable/ic_clear_black_24dp"
android:drawableStart="@drawable/ic_clear_black_24dp"
android:layout_weight="1"
android:gravity="center"
android:background="@color/colorButtonBg"
android:textColor="@color/colorInputTextAccent"
/>
</LinearLayout>
ic_done_black_24dp.xml 可绘制
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M9,16.2L4.8,12l-1.4,1.4L9,19 21,7l-1.4,-1.4L9,16.2z"/>
我看到这里发布了一个答案,此后已被删除。该解决方案应该适合您。
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MirrorMirror"
android:textStyle="bold"
android:drawableLeft="@mipmap/ic_launcher"
android:textAlignment="center"
android:textSize="20sp"
android:id="@+id/toolbar_title"
android:layout_gravity="center"
android:layout_marginRight="10dp"/>
最后一行是应该做的事情。将边距值从 10dp
更改为最适合您正在使用的图像的值。
或
您可以使用锚定到 TextView
的 ImageView
来获得相同的结果。但在这种情况下您将拥有更多控制权,因为可绘制对象的大小不会影响文本的对齐方式。
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="someText"
android:layout_gravity="center"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
app:layout_anchor="@id/toolbar_title"
android:layout_gravity="start|center"
app:layout_anchorGravity="start|center"/>
</android.support.design.widget.CoordinatorLayout>
这就是输出的样子:
编辑
我现在意识到您面临的问题可能是由于使用 ToolBar
时出现的偏移量 (?)。 This 答案为此提供了一个很好的解决方案。它是这样的
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
</android.support.v7.widget.Toolbar>
<TextView
android:id="@+id/toolbar_title"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MirrorMirror"
android:textStyle="bold"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:layout_toLeftOf="@id/toolbar_title"/>
</RelativeLayout>
在这里我不是将TextView
和ImageView
放在Toolbar
里面因为它会再次稍微向右偏移。相反,我将它放在 上面 Toolbar