簡體   English   中英

以編程方式在列表視圖中設置textview的邊距/填充

[英]programmatically set margin/ padding of textview in a listview

這是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);

正如您所看到的,我使用了三種方法在列表視圖中設置textview的邊距/填充但不成功。

我使用的是xml textview而不是create new。

我想手動設置,因為我得到if / else語句,因為沒有發布。

我完全不知道如何在java中設置它。

請給我一個可行的想法,謝謝

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

maintitle.setPadding(50, 0, 0, 0); (方法2)應該工作。

我認為問題在於,盡管你對TextView進行了很多更改,但最后還是會再次膨脹新的布局:

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

所以,制作一個自定義適配器,然后在getView()充氣和自定義TextView

小心,(int)像素!= dp

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

嘗試以下代碼,它適用於我。

textView.setX(40);

我的解決方案適用於SDK 14及更高版本。 不在ListView中,而是在RecyclerView中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM