简体   繁体   English

删除TextView的上边距

[英]Remove top margin of a TextView

I've got some LinearLayout objects with several things in them, including two TextView s. 我有一些带有一些东西的LinearLayout对象,包括两个TextView Unfortunately, I've got a huge space between them and I don't know why. 不幸的是,它们之间有很大的空间,我不知道为什么。

See by yourself (between the content frame and the #642954 - 14/25/9814 thing): 自己看看(在内容框架和#642954 - 14/25/9814 ):

http://i.stack.imgur.com/832QN.png http://i.stack.imgur.com/832QN.png

Here is my code: 这是我的代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >

    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:contentDescription="@string/smiley"
        android:padding="10dp"
        />

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="1"
        android:includeFontPadding="false"
        >

        <TextView android:id="@+id/contenu"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent"
             android:background="@drawable/round"
             android:padding="10dp"
             android:layout_margin="5dp"
             android:includeFontPadding="false"
             android:layout_marginBottom="-10dp"
             />


        <TextView android:id="@+id/infos"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent"
             android:gravity="right"
             android:layout_margin="5dp"
             android:includeFontPadding="false"
             android:padding="0dp"
             android:layout_marginTop="-10dp"
             />

    </LinearLayout>

</LinearLayout>

Here is round.xml : 这是round.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <shape android:shape="rectangle" >
            <solid android:color="#20000000" />

            <corners android:radius="13dp" />
        </shape>
    </item>
    <item android:bottom="2px">
        <shape xmlns:android="http://schemas.android.com/apk/res/android" >
            <solid android:color="#FFFFFF" />

            <corners android:radius="13dp" />

            <stroke
                android:width="1dp"
                android:color="#BBBBBB" />
        </shape>
    </item>

</layer-list>

Could you help me? 你可以帮帮我吗?

Thanks! 谢谢!

android:includeFontPadding =“ false”

No, no, no. 不不不。 You use negative margins, which may have unspecified behavior (presumably depending on Android OS version). 您使用负边距,这可能具有未指定的行为(可能取决于Android OS版本)。 I've tested this on API 8, and negative margins simply don't work. 我已经在API 8上对此进行了测试,并且负边距根本不起作用。 Instead, set each and every margin to a custom non-negative value, like so: 而是将每个边距都设置为自定义非负值,如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >

    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:contentDescription="@string/smiley"
        android:padding="10dp"
        />

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="1"
        android:includeFontPadding="false"
        >

        <TextView
            android:id="@+id/contenu"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginBottom="0dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:background="@drawable/round"
            android:includeFontPadding="false"
            android:padding="10dp"
            android:text="" />

        <TextView
            android:id="@+id/infos"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="0dp"
            android:gravity="right"
            android:includeFontPadding="false"
            android:padding="0dp"
            android:text="" />

    </LinearLayout>

</LinearLayout>

Here, I've set all margins to 5dp except for: 在这里,我将所有边距设置为5dp除了:

  • contenu 's bottom margin: 0dp contenu的底部边距: 0dp
  • infos ' top margin: 0dp infos的最高边距: 0dp

And voila, all the space is gone. 瞧,所有空间都消失了。 Set them to your liking to adjust the gap between first and second TextView . 根据自己的喜好设置它们,以调整第一和第二TextView之间的距离。

Edit 编辑

I've checked SDK sources for unspecified behavior I was writing above. 我已经检查了SDK来源是否存在我在上面编写的未指定的行为

It turns out that when specifying margins for LinearLayout children, you have two options one of which must you choose: 事实证明,在为LinearLayout子级指定边距时,您有两个选项必须选择:

  1. Set margin property. 设置margin属性。 This effectively sets all side margins to the value of margin . 这有效地将所有边距设置为margin的值。 Beware: This option has precedence, meaning, if you set non-negative value for margin it will ignore any values which you have individually set for any side margins. 注意:此选项具有优先权,这意味着,如果为margin设置了非负值,它将忽略为各个边距单独设置的任何值。
  2. Set every side margin individually. 分别设置每个边距。

You were setting global margin value which effectively caused the marginTop / marginBottom values to be ignored. 您正在设置全局margin值,该值实际上导致marginTop / marginBottom值被忽略。

try this for equal spacing 试试这个等距

   <TextView android:id="@+id/contenu"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent"
             android:background="@drawable/round"
             android:padding="10dp"
             android:layout_margin="5dp"
             android:includeFontPadding="false"
             android:layout_marginTop="5dp"
             />


        <TextView android:id="@+id/infos"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent"
             android:gravity="right"
             android:layout_margin="5dp"
             android:includeFontPadding="false"
             android:padding="0dp"
             android:layout_marginTop="5dp"
             />

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

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