【问题标题】:Android: Top and bottom padding of text in listview itemAndroid:列表视图项中文本的顶部和底部填充
【发布时间】:2014-04-13 03:43:56
【问题描述】:

我正在开发一个使用自定义列表视图的应用程序。 XML 代码如下:

<?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="@drawable/loginbg" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_margin="15sp"
        android:background="@android:color/white" >

        <ListView
            android:id="@+id/listview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:divider="#000000"
            android:dividerHeight=".5sp" >
        </ListView>
    </RelativeLayout>

</RelativeLayout>

我的自定义适配器 XML 是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView 
        android:id="@+id/textview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingTop="75sp"
        android:paddingBottom="75sp"
        android:gravity="center"
        android:textSize="20sp"
        android:textColor="@android:color/white" />

</RelativeLayout>

但我的输出并不像我需要的那样。我需要在 XML 适配器 textview 中给出的文本垂直填充。

我的输出是:

我需要列表视图适配器中文本视图中顶部和底部文本的空间。

我也尝试从 .java 文件:

textView.setPadding(0, 75, 0, 75);

但没用。

我需要这样的输出:

请指导我现在该怎么做。

【问题讨论】:

  • 对于填充使用dp 单位而不是sp(应该只用于字体大小)
  • 好的,但我该怎么办。我现在用 dp。
  • @ManojFegde 使用了 dp,但您没有得到想要的结果?
  • 不,我没有得到结果。

标签: android android-layout listview android-listview android-adapter


【解决方案1】:

然后你不需要顶部和底部,检查这个:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/textviewn_company_clsa"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="75dp"
        android:text="asdf"
        android:textColor="@android:color/white"
        android:textSize="20sp" />

</RelativeLayout>  

对于自定义布局,在您的TextView 内,使用marginTopmarginBottom(或layout_margin),这将为您提供TextView 容器之外的空间

填充提供容器内的空间。对于您的ListViewitem,您也需要外部空间每个row/item

查看:
Android beginner difference between padding and margin
Difference between a View's Padding and Margin

android:paddingTop="75sp"
android:paddingBottom="75sp"  

还有

android:layout_margin="15sp"  
android:dividerHeight=".5sp" 

单位需要在“dp”中..:

android:paddingTop="75dp"
android:paddingBottom="75dp" 

还有

android:layout_margin="15dp"  
android:dividerHeight=".5dp"  

供您参考:
What is the difference between "px", "dp", "dip" and "sp" on Android?
http://www.prandroid.com/2013/03/differences-between-px-dip-dp-and-sp.html

【讨论】:

  • 我完成了更改。但我的输出是一样的。没有变化的影响。请指导我。
  • 请查看我的编辑后发布。我粘贴了我需要的输出。
  • 换句话说,margin 和 padding 是一样的:它们在你的东西和其他东西之间留出空间,但是 padding 用你的视图填充空间,而 margin 使空间留空。
【解决方案2】:

使用这个:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="60dp" >

    <TextView 
        android:id="@+id/textviewn_company_clsa"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:textSize="20sp"
        android:textColor="@android:color/white" />

</RelativeLayout>

【讨论】:

  • 请看我的帖子。我添加了我需要的输出。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-16
  • 2011-11-02
  • 1970-01-01
  • 2014-01-28
  • 2018-04-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多