简体   繁体   English

如何在布局中将TextView与背景对齐-Android

[英]How to align TextView with background in Layout - Android

I've got a small problem. 我有一个小问题。

I've got: 我有:

在此处输入图片说明

I want: (but the text few dp from the left) 我想要:(但文字从左数dp)

在此处输入图片说明

Is it possible? 可能吗? Here is a whole code of my layout: (@+id/txtname is the textview from the screenshot!) 这是我布局的完整代码: (@ + id / txtname是屏幕截图中的textview!)

    <?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/md_brown_100"
    android:orientation="vertical"
    android:gravity="center"
    android:paddingTop="10dp"
    android:paddingBottom="10dp" >


    <LinearLayout android:id="@+id/list_offer_item_container"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:background="@drawable/shape"
        android:gravity="center"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:gravity="center"

            android:layout_gravity="center_vertical">

            <TextView android:id="@+id/list_offer_badge_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_above="@+id/list_offer_item_container"
                android:textColor="@color/md_amber_700"
                android:textSize="12sp"
                android:visibility="gone"
                android:text=""/>
        </LinearLayout>

        <LinearLayout
            android:layout_height="150dp"
        android:orientation="vertical"
        android:padding="5dp"
            android:weightSum="1"
            android:layout_width="150dp">

            <ImageView
                android:id="@+id/imgcreditcompany"
                android:scaleType="fitXY"
                android:layout_gravity="center_horizontal"

                android:layout_above="@+id/txtname"
                  android:contentDescription="@string/app_name"
                android:layout_height="48dp"
                android:layout_width="48dp" />


            <TextView
                android:id="@+id/txtname"
                android:gravity="center_vertical"
                android:background="@drawable/ic_dollar_ss"
                android:layout_height="30dp"
                android:layout_marginTop="5dp"
                android:textColor="@color/AliceBlue"
                android:textSize="18sp"
                android:text="Name"
                android:textAllCaps="false"
                android:textStyle="normal|bold"
                android:layout_width="153dp" />

            <TextView
                android:id="@+id/txtdesc"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:layout_marginTop="1dp"
                android:maxLines="3"

                android:textColor="#80869c"
                android:textSize="@dimen/textsizeearncredit_desc"
                android:text="This is a description of the offer and this is just a demo to show off 3 lines stacking correctly on top of each other"
                android:layout_weight="0.55"
                android:layout_width="140dp" />
        </LinearLayout>

        <ImageView
            android:id="@+id/nextArrow"
            android:layout_alignParentRight="true"
            android:tint="@color/md_grey_300"
            android:rotation="180"
            android:layout_width="32dp"
            android:visibility="gone"
            android:layout_marginRight="16dp"
            android:layout_gravity="center"
            android:layout_centerVertical="true"
            android:layout_height="32dp"
            android:contentDescription="@string/app_name"
            android:padding="@dimen/two" />

    </LinearLayout>


</RelativeLayout>

Please help me guys! 请帮助我! Thanks a lot of You can give me any idea of it. 非常感谢您可以给我任何想法。 Cheers! 干杯!

The problem is that your LinearLayout has a padding of 5dp, which keeps your "Invite Friends" TextView away from the edge. 问题是您的LinearLayout的填充为5dp,从而使“邀请朋友” TextView远离边缘。

Replace 更换

<LinearLayout
        android:layout_height="150dp"
        android:orientation="vertical"
        android:padding="5dp"
        android:weightSum="1"
        android:layout_width="150dp">

with

<LinearLayout
        android:layout_height="150dp"
        android:orientation="vertical"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:paddingEnd="5dp"
        android:weightSum="1"
        android:layout_width="150dp">

This way you will have no padding on the left. 这样,您将在左侧没有填充。 To compensate it on the description text, add android:paddingStart="5dp" to your TextView txtdesc 为了补偿描述文字,请将android:paddingStart="5dp"到TextView txtdesc

Although im fairly new to Android Studio, i'd recommend you to try changing the layout gravity property on that specific TextView to adjust itself to the left corner and/or try forcing a padding to the left aswell. 尽管对Android Studio来说还不是很新,但我还是建议您尝试更改该特定TextView上的layoutgravity属性,以将其自身调整到左角,和/或尝试也将填充物向左强制。

Though this is just a guess of what could solve it, i hope it helps 尽管这只是解决方案的猜测,但我希望它能对您有所帮助

将该视图的布局重力设置为android:layout_gravity="left"

solution:--- Please add following property within name textview . 解决方案:---请在名称textview中添加以下属性。 android:gravity="center_horizontal"

your code: 您的代码:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingTop="10dp">


<LinearLayout
    android:id="@+id/list_offer_item_container"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:gravity="center"
    android:paddingBottom="10dp"
    android:paddingTop="10dp">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:gravity="center"

        android:orientation="vertical">

        <TextView
            android:id="@+id/list_offer_badge_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/list_offer_item_container"
            android:text=""
            android:textSize="12sp"
            android:visibility="gone" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:orientation="vertical"
        android:padding="5dp"
        android:weightSum="1">

        <ImageView
            android:id="@+id/imgcreditcompany"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:layout_above="@+id/txtname"
            android:layout_gravity="center_horizontal"
            android:contentDescription="@string/app_name"
            android:scaleType="fitXY" />


        <TextView
            android:id="@+id/txtname"
            android:layout_width="153dp"
            android:layout_height="30dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="5dp"
            android:gravity="center_horizontal"
            android:text="Name"
            android:textAllCaps="false"
            android:textSize="18sp"
            android:textStyle="normal|bold" />

        <TextView
            android:id="@+id/txtdesc"
            android:layout_width="140dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="1dp"
            android:layout_weight="0.55"

            android:gravity="center_horizontal"
            android:maxLines="3"
            android:text="This is a description of the offer and this is just a demo to show off 3 lines stacking correctly on top of each other"
            android:textColor="#80869c" />
    </LinearLayout>

    <ImageView
        android:id="@+id/nextArrow"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_gravity="center"
        android:layout_marginRight="16dp"
        android:contentDescription="@string/app_name"
        android:rotation="180"
        android:visibility="gone" />

</LinearLayout>

The padding attribute for the linear layout encompassing the text and image view should be set to 0. 包含文本和图像视图的线性布局的padding属性应设置为0。

android:padding="5dp"

This includes paddings left & right, hence the purple colour has a slight gap, remove the padding and the purple shape will reach the sides of the container shape. 这包括左右的填充物,因此紫色略有间隙,去掉填充物,紫色形状将到达容器形状的侧面。

I noticed you only want the purple shape to touch the RHS, hence we only want to remove the paddingLeft. 我注意到您只希望紫色形状触摸RHS,因此我们只希望删除paddingLeft。

  android:paddingRight="5dp"
  android:paddingLeft="0dp"
  android:paddingBottom="5dp"
  android:paddingTop="5dp"

The TextView's gravity attribute must then be set to 'left' 然后必须将TextView的gravity属性设置为“ left”

  android:gravity="left"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM