0
因此,我正在迭代Strings和Booleans的HashMap
。我爲每個String在LinearLayout上放置一個TextView。這很好。我需要做的是將布爾值的TextView放置到每個String TextView的右邊。有任何想法嗎?這就是我要尋找...以編程方式在LinearLayout中並排設置兩個TextView
LinearLayout planLayout = (LinearLayout) findViewById(R.id.planLayout);
for (Map.Entry<String, Boolean> entry : plans.entrySet()) {
String key = entry.getKey();
Boolean value = entry.getValue();
TextView keyTV = new TextView(this);
keyTV.setText(key + " | ");
// here is where I want to set a TextView of the Boolean to the right of the keyTV
planLayout.addView(keyTV);
}
使用ClickableSpan
LinearLayout planLayout = (LinearLayout) findViewById(R.id.planLayout);
for (Map.Entry<String, Boolean> entry : plans.entrySet()) {
String key = entry.getKey();
Boolean value = entry.getValue();
TextView keyTV = new TextView(this);
SpannableString ss = new SpannableString(value.toString());
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View textView) {
Toast.makeText(getApplicationContext(), "clicked",
Toast.LENGTH_SHORT).show();
System.out.println("Hello");
}
};
ss.setSpan(clickableSpan, 0, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
keyTV.setText(key + " | " + ss);
keyTV.setMovementMethod(LinkMovementMethod.getInstance());
keyTV.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
planLayout.addView(keyTV);
}
}
我已經這樣做了,但右側(布爾值)最終將是可點擊的,其中左側不會是。因此,我需要將它們分開 – Harry 2014-11-04 20:34:19
感謝ClickableSpan上的提示。將檢查出來。 – Harry 2014-11-04 20:36:58
@你可以使用spanned字符串來僅使textview的一部分可點擊。或者,也可以嵌套佈局。 – matiash 2014-11-04 20:37:00