我试图找到一种动态创建自定义视图的方法,但我没有为我的问题找到有用的帖子。
我有下一个观点。
这是布局的树:
我想创建与收到的项目一样多的副本,但我需要更改每个项目内的文本视图。
我不知道如果我解释得很好。谢谢。
编辑:
我遵循你的一些建议,我这样做:
private void addInputHeader(int timestamp)
{
LinearLayout mRootLayout = (LinearLayout) findViewById(R.id.event_container);
View mChildView = getLayoutInflater().inflate(R.layout.program_event_day_header, mRootLayout, false);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
mRootLayout.addView(mChildView, params);
TextView tv_day = (TextView) mChildView.findViewById(R.id.tv_day);
tv_day.setText(getDayForHeader(timestamp) + getResources().getString(R.string.of) + getMonthForHeader(timestamp));
}
private void addInputDescription(int timestamp, String description)
{
LinearLayout mRootLayout = (LinearLayout) findViewById(R.id.event_container);
View mChildView = getLayoutInflater().inflate(R.layout.program_event_day_header, mRootLayout, false);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
mRootLayout.addView(mChildView, params);
TextView tv_event_hour = (TextView) mChildView.findViewById(R.id.tv_event_hour);
TextView tv_event_message = (TextView) mChildView.findViewById(R.id.tv_event_message);
tv_event_hour.setText(getHour(timestamp) + " - ");
tv_event_message.setText(description);
}
private String getDayForHeader(long timeStamp){
Date df = new Date(timeStamp*1000);
return new SimpleDateFormat("cccc dd").format(df);
}
private String getMonthForHeader(long timeStamp){
Date df = new Date(timeStamp*1000);
return new SimpleDateFormat("MM").format(df);
}
private String getHour(long timeStamp){
Date df = new Date(timeStamp*1000);
return new SimpleDateFormat("HH:mm").format(df);
}
private int getD(long timeStamp){
Date df = new Date(timeStamp*1000);
return Integer.parseInt(new SimpleDateFormat("dd").format(df));
}
private int getM(long timeStamp){
Date df = new Date(timeStamp*1000);
return Integer.parseInt(new SimpleDateFormat("MM").format(df));
}
private int getY(long timeStamp){
Date df = new Date(timeStamp*1000);
return Integer.parseInt(new SimpleDateFormat("yyyy").format(df));
}
但是当addInputDescription尝试在tv_event_hour上设置文本时,我收到NullPointerException。
答案 0 :(得分:1)
为您的商品创建一个VO并将其添加到布局中。 比如,layout.add(item)
答案 1 :(得分:1)
如果您尝试将子视图(或多个)添加到根LinearLayout
,请参阅以下代码:
LinearLayout mRootLayout = (LinearLayout) findViewById(R.id.ll_root);
View mChildView = getLayoutInflater().inflate(R.layout.ll_child, mRootLayout,
false);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
mRootLayout.addView(mChildView, params);
TextView tvEventHour = (TextView) mChildView.findViewById(R.id.tv_event_hour);
TextView tvEventMessage = (TextView) mChildView.findViewById(R.id.tv_event_message);
tvEventHour.setText("10:30");
tvEventMessage.setText("Fugia corum...");
注意:如果列表需要很长时间,您应该考虑使用带有适配器的ListView,因为这样会有更好的性能。
答案 2 :(得分:1)
//添加代表ui row.xml的行视图布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/leftTop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Left Top" />
<TextView
android:id="@+id/RightTop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Right Top" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/leftBottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Left Bottom" />
<TextView
android:id="@+id/RightBottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Right Bottom" />
</LinearLayout>
</LinearLayout>
//使用msg
添加视图作为行 private void addMsg() {
View msgView = getViewWithMsg("TopLeft", "TopRight", "LeftBottom", "Right Bottom");
// msgView represent one row
// You can add it to linearlayout desendent to scrollview
}
//此方法创建一个msg行布局并更新msg
private View getViewWithMsg(String TopLeft, String TopRight, String LeftBottom,
String RightBottom) {
LayoutInflater inflator = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View root = inflator.inflate(R.layout.row, null);
TextView topLeft = (TextView) root.findViewById(R.id.leftTop);
TextView topRight = (TextView) root.findViewById(R.id.RightTop);
TextView bottomLeft = (TextView) root.findViewById(R.id.leftBottom);
TextView botomRight = (TextView) root.findViewById(R.id.RightBottom);
topLeft.setText(TopLeft);
topRight.setText(TopRight);
bottomLeft.setText(LeftBottom);
botomRight.setText(RightBottom);
return root;
}
答案 3 :(得分:0)
你可以从xml充气:
View placeHolderView = inflater.inflate(R.layout.view_header_placeholder, mListView);
你是什么意思&#34;更改每个项目内的文本视图。&#34;? 如果你需要在膨胀的布局中的textview尝试这个:
TextView myTextView = (TextView)placeHolderView.findViewById(R.id.my_text_view);