首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在列表视图中以编程方式设置文本视图的边距/填充

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

Stack Overflow用户
提问于 2012-05-08 10:48:38
回答 3查看 72.3K关注 0票数 21

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

正如您所看到的,我使用了的三个方法来设置列表视图中的文本视图的边距/填充,但没有成功。

我正在使用xml文本视图,而不是创建新的。

我想手动设置,因为我在那里得到了if / else语句,因为它没有post out。

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

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

((MarginLayoutParams) maintitle.getLayoutParams()).leftMargin = margin;
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10491922

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档