以编程方式在列表视图中设置textview的边距/填充

时间:2012-05-08 02:48:38

标签: android padding margin

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

正如您所看到的,我使用 THREE 方法在列表视图中设置textview的边距/填充但不成功。

我正在使用xml textview而不是create new。

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

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

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

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

3 个答案:

答案 0 :(得分:34)

maintitle.setPadding(50, 0, 0, 0);(方法2)应该有效。

我认为问题在于,尽管您对TextView进行了大量更改,但最终还是会在以下情况下再次展开新布局:

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

所以,制作一个自定义适配器,然后在TextView中充气并自定义getView()

答案 1 :(得分:4)

小心,(int)像素!= dp

#include <boost/algorithm/cxx11/any_of.hpp>
#include <boost/range/adaptors.hpp>

for(auto const& p : 
      boost::make_iterator_range(fs::directory_iterator(srcpath), {})
      | boost::adaptors::transformed([](auto const& d) { 
          return fs::path(d); })
      | boost::adaptors::filtered([](auto const& p) { 
          return fs::is_regular_file(p); })
      | boost::adaptors::filtered([](auto const& p) { 
          auto const& exts = { ".txt", ".csv" };
          return boost::algorithm::any_of_equal(exts, p.extension().string()); })
   ) {
    // all filenames here will have one of the extensions you tested for
}

答案 2 :(得分:3)

尝试以下代码,它适用于我。

  

textView.setX(40);

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

相关问题