Android 在列表视图中以编程方式设置文本视图的边距/填充
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10491922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
programmatically set margin/ padding of textview in a listview
提问by Alan Lai
This is a part of main_alllatestnews.xml
这是 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>
This is full of main_alllatestnewslist.xml
这个满是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>
This is a part of main_alllatestnews.java
这是 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);
As you can see i had used THREEmethods to set margin/ padding of textview in a listview but not success.
如您所见,我使用了三种方法在列表视图中设置文本视图的边距/填充,但没有成功。
I am using xml textview instead of create new.
我正在使用 xml textview 而不是创建新的。
I want to manually set because i got if / elsestatement there as did not post out.
我想手动设置,因为我在那里得到了if / else语句,因为没有发布。
I am totally out of idea how to set it in java.
我完全不知道如何在java中设置它。
Please provide me a idea that workable one, thanks
请给我一个可行的想法,谢谢
((MarginLayoutParams) maintitle.getLayoutParams()).leftMargin = margin;
回答by C. Leung
maintitle.setPadding(50, 0, 0, 0);
(method 2) should work.
maintitle.setPadding(50, 0, 0, 0);
(方法2)应该有效。
I think the problem is that, although you do a lot of changes to the TextView
, at the end you kind of inflate a new layout again in:
我认为问题在于,尽管您对 进行了大量更改TextView
,但最终您还是在以下内容中再次膨胀了一个新布局:
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps,
R.layout.main_alllatestnewslist, from, to);
so, make a custom adapter, then inflate and customize your TextView
in the getView()
.
因此,制作一个自定义适配器,然后TextView
在getView()
.
回答by Santiago Battaglino
Be careful, (int) pixels != dp
小心,(int)像素!= dp
public int dp2px(int dp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());
}
回答by Chong Chern Dong
Try the following code, it works for me.
试试下面的代码,它对我有用。
textView.setX(40);
textView.setX(40);
My solution is for SDK 14 and above. Not in ListView but RecyclerView.
我的解决方案适用于 SDK 14 及更高版本。不在 ListView 中,而是在 RecyclerView 中。