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

廉价感情. 提交于 2019-12-22 02:05:10

问题


I need to set padding for the TextView in every row of ListView or ExpandableListView. I try to use android:padding and child (paddingLeft,...) but without any results. How can I do? Thanks

EDIT:

This is the code of the item of the 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 TextView doesn't work, even if the padding is set in only one of the 2 tags.

EDIT :

Solved. The problem was in the Java code, in the SimpleExpandableListAdapter declaration. 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.


回答1:


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.

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.




回答2:


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

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



回答3:


You can add padding as follows:

android:paddingBottom="20pt"


来源:https://stackoverflow.com/questions/12250162/android-how-to-set-padding-for-textview-in-listview-or-expandablelistview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!