【问题标题】:programmatically set margin/ padding of textview in a listview以编程方式在列表视图中设置文本视图的边距/填充
【发布时间】:2012-05-16 13:00:10
【问题描述】:

这是 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 而不是新建。

我想手动设置,因为我有 if / else 声明没有发布。

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

请给我一个可行的想法,谢谢

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

【问题讨论】:

  • 您可以在适配器的 getView 中执行此操作,您可以在其中膨胀布局'

标签: android padding margin


【解决方案1】:

试试下面的代码,它对我有用。

textView.setX(40);

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

【讨论】:

    【解决方案2】:

    小心,(int) 像素!= dp

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

    【讨论】:

    • 你到底是什么意思?这个问题已经以最好的方式回答了,可能是这样,这就像你试图放入一些完全不相关和不清楚的东西,这对任何人都没有帮助。
    【解决方案3】:

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

    我认为问题在于,尽管您对 TextView 进行了很多更改,但最后您还是会在以下位置再次填充新布局:

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

    所以,制作一个自定义适配器,然后在getView() 中扩展和自定义您的TextView

    【讨论】:

      猜你喜欢
      • 2015-02-24
      • 2011-04-08
      • 1970-01-01
      • 2013-03-11
      • 2012-07-15
      • 1970-01-01
      • 1970-01-01
      • 2016-01-11
      • 2011-05-27
      相关资源
      最近更新 更多