在ListView中以编程方式设置TextView的边距/填充。

22

这是main_alllatestnews.xml的一部分。

<LinearLayout
    android:id="@+id/layout_content"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/layout_menu"
    android:layout_below="@id/layout_title"
    android:orientation="vertical" >
    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </ListView>
</LinearLayout>

这是主要的所有最新新闻列表main_alllatestnewslist.xml。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_temp"
android:layout_width="fill_parent"
android:layout_height="100px"
android:background="@drawable/background_news_list" >

<ImageView
    android:id="@+id/image_alllatestnewstitle"
    android:layout_width="134px"
    android:layout_height="80px"
    android:layout_marginBottom="5px"
    android:layout_marginLeft="10px"
    android:layout_marginRight="10px"
    android:layout_marginTop="5px"
    android:scaleType="centerCrop" />

<LinearLayout
    android:id="@+id/test"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/text_particularlatestnewstitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:textSize="25px" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="25px" >

        <TextView
            android:id="@+id/text_newsdate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#999999"
            android:textSize="15px" />

        <TextView
            android:id="@+id/text_newscategorytitle"
            android:layout_width="50px"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_gravity="right"
            android:gravity="right"
            android:textColor="#ff0000"
            android:textSize="15px" />
    </RelativeLayout>
</LinearLayout>

这是main_alllatestnews.java的一部分

LayoutInflater liInflater = (LayoutInflater) this
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    linear.addView(liInflater
            .inflate(R.layout.main_alllatestnewslist, null));
lv = (ListView) findViewById(android.R.id.list);
for (int i = 0; i < webservice.news.size(); i++) {
    maintitle = (TextView) findViewById(R.id.text_particularlatestnewstitle);
    LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams)maintitle.getLayoutParams(); 
    layoutParams.setMargins(50, 0, 0, 0);       <-- method one

    maintitle.setPadding(50, 0, 0, 0);          <-- method two
    maintitle.setLayoutParams(layoutParams);

    int margin = 100;                           <-- method three
    ((MarginLayoutParams) maintitle.getLayoutParams()).leftMargin = margin;
}
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps,
            R.layout.main_alllatestnewslist, from, to);
    lv.setAdapter(adapter);

你可以看到我已经试用了三种方法来设置ListView中TextView的margin/padding,但没有成功。

我在使用XML TextView而不是新创建一个TextView。

我想手动设置它,因为我有if/else语句,但我没有公布。

我完全没有主意如何在Java中设置它。

请提供一个可行的想法,谢谢。

((MarginLayoutParams) maintitle.getLayoutParams()).leftMargin = margin;

2
你可以在适配器(Adapter)的getView方法中完成这个操作,其中你需要先加载布局文件。 - Its not blank
3个回答

34

maintitle.setPadding(50, 0, 0, 0);(方法2)应该可以工作。

我认为问题在于,虽然您对TextView进行了许多更改,但最后你又在以下位置再次填充了一个新的布局:

SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, 
            R.layout.main_alllatestnewslist, from, to);

因此,创建一个自定义适配器,然后在getView()中填充和自定义你的TextView


5

注意,(int) 像素不等于 dp

public int dp2px(int dp) {
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());
}

3
你的意思是什么?这个问题已经以最好的方式得到了回答,现在你试图加入一些完全不相关和不清楚的东西,这对任何人都没有帮助。 - Praveen Kumar Purushothaman

4

请尝试以下代码,它对我有效。

textView.setX(40);

我的解决方案适用于SDK 14及以上版本。不是在ListView中而是在RecyclerView中。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接