listView 加入表头,为什么会重叠在一起?

xhz8000 2017-01-20 10:52:29
package com.example.testandroid;

import android.content.Context;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class lvheader extends LinearLayout {
private Context lcontext;
private RelativeLayout mReLayout = null;
public lvheader(Context context) {
super(context);
this.lcontext = context;
this.setOrientation(LinearLayout.HORIZONTAL);
mReLayout = new RelativeLayout(context);
CreateHeader();
}

private void CreateHeader()
{
LinearLayout.LayoutParams lpParam = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, 50);
RelativeLayout.LayoutParams reParam = new RelativeLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);

int cl = this.getResources().getColor(android.R.color.holo_orange_light);
mReLayout.setBackgroundColor(cl);

TextView Tv1 = new TextView(lcontext);
Tv1.setWidth(100);
Tv1.setHeight(50);
Tv1.setText("序号");
Tv1.setId(1);
mReLayout.addView(Tv1, reParam);

TextView Tv2 = new TextView(lcontext);
Tv2.setWidth(100);
Tv2.setHeight(50);
Tv2.setText("报表ID");
Tv2.setId(2);
reParam.addRule(RelativeLayout.RIGHT_OF, Tv1.getId());
mReLayout.addView(Tv2, reParam);

TextView Tv3 = new TextView(lcontext);
Tv3.setWidth(150);
Tv3.setHeight(50);
Tv3.setId(3);
Tv3.setText("报表名称");
reParam.addRule(RelativeLayout.RIGHT_OF, Tv2.getId());
mReLayout.addView(Tv3, reParam);

TextView Tv4 = new TextView(lcontext);
Tv4.setWidth(100);
Tv4.setHeight(50);
Tv4.setId(4);
Tv4.setText("备注");
reParam.addRule(RelativeLayout.RIGHT_OF, Tv3.getId());
mReLayout.addView(Tv4, reParam);

this.addView(mReLayout, lpParam);

}

}
...全文
166 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
寒冰大神 2017-01-20
  • 打赏
  • 举报
回复
reParam.addRule(RelativeLayout.RIGHT_OF, Tv1.getId()); 没起作用吧 把 mReLayout 变成 LinearLayout 就更好实现了。 import android.content.Context; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.TextView; public class lvheader extends LinearLayout { private Context lcontext; private LinearLayout mReLayout = null; public lvheader(Context context) { super(context); this.lcontext = context; this.setOrientation(LinearLayout.HORIZONTAL); mReLayout = new LinearLayout(context); CreateHeader(); } private void CreateHeader() { LinearLayout.LayoutParams lpParam = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, 50); LinearLayout.LayoutParams reParam = new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT); int cl = this.getResources().getColor(android.R.color.holo_orange_light); mReLayout.setBackgroundColor(cl); mReLayout.setOrientation(LinearLayout.HORIZONTAL); TextView Tv1 = new TextView(lcontext); Tv1.setWidth(100); Tv1.setHeight(50); Tv1.setText("序号"); Tv1.setId(1); mReLayout.addView(Tv1, reParam); TextView Tv2 = new TextView(lcontext); Tv2.setWidth(100); Tv2.setHeight(50); Tv2.setText("报表ID"); Tv2.setId(2); // reParam.addRule(RelativeLayout.RIGHT_OF, Tv1.getId()); mReLayout.addView(Tv2, reParam); TextView Tv3 = new TextView(lcontext); Tv3.setWidth(150); Tv3.setHeight(50); Tv3.setId(3); Tv3.setText("报表名称"); // reParam.addRule(RelativeLayout.RIGHT_OF, Tv2.getId()); mReLayout.addView(Tv3, reParam); TextView Tv4 = new TextView(lcontext); Tv4.setWidth(100); Tv4.setHeight(50); Tv4.setId(4); Tv4.setText("备注"); // reParam.addRule(RelativeLayout.RIGHT_OF, Tv3.getId()); mReLayout.addView(Tv4, reParam); this.addView(mReLayout, lpParam); } }
xhz8000 2017-01-20
  • 打赏
  • 举报
回复
//感谢楼上的大哥在你给的提示下修改一下 package com.example.testandroid; import android.content.Context; import android.graphics.Color; import android.view.Gravity; import android.widget.LinearLayout; import android.widget.TextView; public class lvheader extends LinearLayout { private Context mContext; public lvheader(Context context) { super(context); this.mContext = context; this.setOrientation(LinearLayout.HORIZONTAL); CreateHeader(); } private void CreateHeader() { LinearLayout.LayoutParams lpParam = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, 100); int cl = Color.rgb(0Xee, 0Xaa, 0X99); this.setBackgroundColor(cl); TextView Tv1 = new TextView(mContext); Tv1.setWidth(200); Tv1.setHeight(80); Tv1.setText("序号"); Tv1.setGravity(Gravity.CENTER); Tv1.setTextSize(16); Tv1.setId(1); this.addView(Tv1, lpParam); TextView Tv2 = new TextView(mContext); Tv2.setWidth(300); Tv2.setHeight(80); Tv2.setGravity(Gravity.CENTER); Tv2.setText("报表ID"); Tv2.setId(2); Tv2.setTextSize(16); this.addView(Tv2, lpParam); TextView Tv3 = new TextView(mContext); Tv3.setWidth(350); Tv3.setHeight(80); Tv3.setGravity(Gravity.CENTER); Tv3.setId(3); Tv3.setText("报表名称"); Tv3.setTextSize(16); this.addView(Tv3, lpParam); TextView Tv4 = new TextView(mContext); Tv4.setWidth(300); Tv4.setHeight(80); Tv4.setGravity(Gravity.CENTER); Tv4.setId(4); Tv4.setText("备注"); Tv4.setTextSize(16); this.addView(Tv4, lpParam); } }

80,337

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧