android 填充内存方法,android - 以编程方式在列表视图中设置textview的边距/填充 - 堆栈内存溢出...

本文档探讨了在Android中为ListView的TextView元素设置边距的挑战。作者展示了尝试在main_alllatestnews.xml布局文件和main_alllatestnews.java代码中设置TextView边距的不同方法,包括使用LayoutParams和MarginLayoutParams,但未能成功。问题在于试图在Java代码中动态设置TextView的内边距和外边距,而没有得到期望的效果。博客提供了相关的XML布局片段和Java代码片段以供参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这是main_alllatestnews.xml的一部分

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" >

android:id="@android:id/list"

android:layout_width="fill_parent"

android:layout_height="wrap_content" >

这里充满了main_alllatestnewslist.xml

android:id="@+id/layout_temp"

android:layout_width="fill_parent"

android:layout_height="100px"

android:background="@drawable/background_news_list" >

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" />

android:id="@+id/test"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="vertical" >

android:id="@+id/text_particularlatestnewstitle"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:textColor="#000000"

android:textSize="25px" />

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_marginTop="25px" >

android:id="@+id/text_newsdate"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textColor="#999999"

android:textSize="15px" />

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" />

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

maintitle.setPadding(50, 0, 0, 0);

maintitle.setLayoutParams(layoutParams);

int margin = 100;

((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;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值