所以我希望有一个X编号的TextView,它以编程方式创建并添加到其parrent布局中,并使用不同的文本设置每个textView取决于其id。
我已经设法创建并在其parrent布局中添加了X个TextView,虽然我不确定我是否设法为每个人提供不同的id,我用简单的字符串操作手动创建它们并将它们存储在HashMap中。
下一步,我尝试使用来自HashMap的id和每个TextView的setText获取findViewById()
的每个TextView,但我不知道为什么我的TextView仍然是空白的。我已经尝试在创建TextView时直接对文本进行硬编码,但它仍然给我空白视图,但这似乎只发生在TextView上,我也尝试以编程方式添加X编号ImageView,方法与添加TextView相同,而setImageResourse依赖于每个id,它的工作原理
我使用的是最小的SDK Jelly Bean 4.2.2(API 17)
这是我的onCreate()
int allEventNumber,imageId,textNameId,textDateId,textTappedId = -1;
LinearLayout layoutUtama,eventLayout,detailLayout;
ImageView thumbnailImageView;
TextView textViewName,textViewDate,textViewTapped;
String imageIdKey,textNameIdKey,textDateIdKey,textTappedIdKey,textDate,textTapped,textName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event);
HashMap<String,Integer> keyId = new HashMap<String,Integer>();
keyId = initAll(keyId);
fillAllById(allEventNumber,keyId);
}
initAll(HashMap中)
public HashMap<String,Integer> initAll(HashMap<String,Integer> hMap){
allEventNumber = 6; //hardcode
layoutUtama = (LinearLayout) findViewById(R.id.layout_utama);
hMap = insertLayout(hMap,allEventNumber);
return hMap;
}
insertLayout(HashMap,int)
public HashMap<String,Integer> insertLayout(HashMap<String ,Integer> hMap, int jumlahEvent){
//
for (int i =1;i<= jumlahEvent;i++){
eventLayout = new LinearLayout(this);
eventLayout.setOrientation(LinearLayout.HORIZONTAL);
eventLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT));
thumbnailImageView = new ImageView(this);
imageIdKey = "key_image_view_" + i;
imageId = View.generateViewId();
hMap.put(imageIdKey,imageId);
thumbnailImageView.setId(imageId);
thumbnailImageView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));
detailLayout = new LinearLayout(this);
detailLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
layoutParams.setMargins(15,0,0,0);
detailLayout.setLayoutParams(layoutParams);
detailLayout.setGravity(Gravity.CENTER);
textViewName = new TextView(this);
textNameIdKey = "key_text_view_name_" + i;
textNameId = View.generateViewId();
hMap.put(textNameIdKey,textNameId);
textViewName.setId(textNameId);
textViewName.setGravity(Gravity.LEFT);
textViewName.setTextSize(R.dimen.event_name_text_size);
textViewName.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
detailLayout.addView(textViewName);
eventLayout.addView(thumbnailImageView);
eventLayout.addView(detailLayout);
layoutUtama.addView(eventLayout);
}
return hMap;
}
fillAllById(INT,HashMap中)
public void fillAllById(int allEventNumber,HashMap<String,Integer> hMap){
for (int i=1;i<=allEventNumber;i++){
imageIdKey = "key_image_view_" + i;
textNameIdKey = "key_text_view_name_" + i;
imageId = hMap.get(imageIdKey);
textNameId = hMap.get(textNameIdKey);
thumbnailImageView = (ImageView) findViewById(imageId);
textViewName = (TextView) findViewById(textNameId);
thumbnailImageView.setImageResource(R.drawable.thumbnail_event3);
textName = "Name Event";
textViewName.setText(textName);
}
}
和我的xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:paddingBottom="0dp"
tools:context="com.example.hansel.tapbandung_v00.Event">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true">
<LinearLayout
android:id="@+id/layout_utama"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp"
android:layout_marginRight="0dp"
android:layout_marginBottom="0dp">
</LinearLayout>
</ScrollView>
</RelativeLayout>