Android - 如何在 ListView 或 ExpandableListView 中为 TextView 设置填充

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12250162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:11:38  来源:igfitidea点击:

Android - How to set padding for TextView in ListView or ExpandableListView

androidlistviewtextviewpaddingexpandablelistview

提问by Andrea Mario Lufino

I need to set padding for the TextViewin every row of ListViewor ExpandableListView. I try to use android:paddingand child (paddingLeft,...) but without any results. How can I do? Thanks

我需要为or 的TextView每一行设置填充。我尝试使用and child ( ,...) 但没有任何结果。我能怎么做?谢谢ListViewExpandableListViewandroid:paddingpaddingLeft

EDIT:

编辑:

This is the code of the item of the ExpandableListView

这是项目的代码 ExpandableListView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="20dp" >

    <TextView 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="wrap_content"
        android:id="@+id/titleScheda"
        android:text="TextView"
        android:layout_width="wrap_content" 
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:padding="20dp" />

</LinearLayout>

Setting padding on both layout and TextViewdoesn't work, even if the padding is set in only one of the 2 tags.

TextView即使仅在 2 个标签之一中设置了填充,在两个布局上设置填充也不起作用。

EDIT :

编辑 :

Solved. The problem was in the Java code, in the SimpleExpandableListAdapterdeclaration. Using the standard layout it's impossible to customize, but using a custom layout for the expandable content is the solution that allows us to customize anything.

解决了。问题出在 Java 代码中,在SimpleExpandableListAdapter声明中。使用标准布局无法进行自定义,但对可扩展内容使用自定义布局是允许我们自定义任何内容的解决方案。

回答by se_bastiaan

I assume you're adding the items in the listview using a template file (xml layout). If you're not, you have to implement that first.

我假设您使用模板文件(xml 布局)在列表视图中添加项目。如果不是,则必须首先实施。

In that layout file you could do this:

在该布局文件中,您可以执行以下操作:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp" >
    <TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text" />
</LinearLayout>

The LinearLayout will create the padding and wrap around the TextView.

LinearLayout 将创建填充并环绕 TextView。

回答by Yalla T.

for(int i=0; i< listview.getChildCount();i++) 

  listview.getChildAt(i).setPadding(10,10,10,10);

回答by pollaris

You can add padding as follows:

您可以按如下方式添加填充:

android:paddingBottom="20pt"