这是我根据句子中的单词实现textview数量的方法。每个单词都有点击事件或触摸事件。
static ArrayList<TextView> sentence(String[] arr,
LinearLayout linelay_wordIn1) {
if (linelay_wordIn1.getChildCount() > 0)
linelay_wordIn1.removeAllViews();
if (allTextView != null) {
allTextView.remove(txt);
allTextView.clear();
System.out.println("hello remove all textview here");
} else {
System.out.println("hello all textview array is null here");
}
String str1 = "";
for (int i = 0; i < arr.length; i++) {
str1 = str1 + arr[i].toString();
System.out.println(" senctence separte in word " + arr[i]
+ " words" + arr.length);
}
/* listview for getting textview */
System.out.println("sentence " + str1.toString() + "str1 length :: "
+ str1.length());
txt = new TextView[arr.length];
LinearLayout.LayoutParams lp;
lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
lp.gravity = Gravity.CENTER;
// lp.setMarginStart(arr.length);
for (int j = 0; j < arr.length; j++) {
txt[j] = new TextView(contextG);
txt[j].setId(j);
txt[j].setBackgroundResource(Color.TRANSPARENT);
txt[j].setTextSize(60);
txt[j].setTypeface(
Typeface.createFromAsset(contextG.getAssets(), "TIMES.TTF"),
Typeface.BOLD);
txt[j].setText(arr[j]);
txt[j].setLayoutParams(lp);
txt[j].setTextColor(Color.BLACK);
txt[j].setOnTouchListener(myListner);
System.out.println("txt[j]" + j + "id " + txt[j].getId());
allTextView.add(txt[j]); /* add textview into arraylist */
linelay_wordIn1.addView(txt[j], j);
}
return allTextView;
}
我的触控器代码在这里
public static OnTouchListener myListner = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
TextView tv = (TextView) v;
//Layout layout = ((TextView) v).getLayout();
String str = tv.getText().toString();
int x = (int) event.getX();
int y = (int) event.getY();
//if (layout != null) {
//int line = layout.getLineForVertical(y);
//int characterOffset = layout.getOffsetForHorizontal(line, x);
//Log.i("index", "" + characterOffset);
//}
System.out.println(" str of sentence :: " + str.toString());
if (Music.playing()) {
// do nothing
} else {
try {
/* Music renew for memory leak problems */
Music.renewMediaPlayer();
/*--------------------*/
if (str.equalsIgnoreCase("I ")) {
Music.play(contextG, sent_audio1[0]);
} else if (str.equals("like ")) {
Music.play(contextG, sent_audio1[1]);
} else if (str.equals("to ")) {
Music.play(contextG, sent_audio1[2]);
} else if (str.equals("ski.")) {
Music.play(contextG, sent_audio1[3]);
tooltip(tool1, 0, 3, x, y);// tool1 is for guide text
// array
// and 0
// for index
} else if (str.equals("When ")) {
Music.play(contextG, sent_audio2[0]);
tooltip(tool2, 0, 0, x, y);
} else if (str.equals("the ")) {
Music.play(contextG, sent_audio2[1]);
tooltip(tool2, 0, 1, x, y);
} else if (str.equals("snow ")) {
Music.play(contextG, sent_audio2[2]);
tooltip(tool2, 0, 2, x, y);
} else if (str.equals("falls, ")) {
Music.play(contextG, sent_audio2[3]);
} else if (str.equals("head ")) {
Music.play(contextG, sent_audio2[5]);
tooltip(tool2, 1, 5, x, y);
} else if (str.equals("slopes.")) {
float x1 = event.getX();
float y1 = event.getY();
Music.play(contextG, sent_audio2[8]);
tooltip(tool2, 2, 8, x1, y1);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
return true;
}
};
此处:数字单词存储在字符串数组中以在textviews中设置为文本
/* sentence here */
static String[] words1 = { "I ", "like ", "to ", "ski." };
static String[] words2 = { "When ", "the ", "snow ", "falls, ", "I ",
"head ", "to ", "the ", "slopes." };
static String[] words3 = { "My ", "Children ", "join ", "me ", "on ",
"the ", "trip ", "to ", "the ", "hills. " };
static String[] words4 = { "We ", "ride ", "in ", "cable ", "cars ", "to ",
"the ", "top ", "of ", "the ", "hill. " };
static String[] words5 = { "The ", "snow ", "sparkles ", "in ", "the ",
"sunshine ", "as", "we ", "ski ", "the ", "trails. " };
static String[] words6 = { "I ", "pass ", "many ", "novice ", "skiers ",
"as ", "they ", "struggle ", "to ", "stay ", "up. " };
static String[] words7 = { "I ", "try ", "to ", "watch ", "out ", "as ",
"I ", "whiz ", "by. " };
static String[] words8 = { "If ", "I ", "do ", "not, ", "I ", "will ",
"fall ", "too. " };
这里是:在每次点击时在textview数组x和y位置显示另一个toast的方法。
static void tooltip(String[] arr, int toolstringindex, int placeindex,
float x, float y) {
FrameLayout layout = new FrameLayout(contextG);
layout.setBackgroundResource(R.drawable.tool_tip_img);
TextView tv = new TextView(contextG);
// set the TextView properties like color, size etc
tv.setTextColor(Color.BLACK);
tv.setTextSize(40);
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
android.widget.FrameLayout.LayoutParams.WRAP_CONTENT,
android.widget.FrameLayout.LayoutParams.WRAP_CONTENT,
Gravity.TOP);
lp.leftMargin = 10;
tv.setLayoutParams(lp);
// set the text you want to show in Toast
tv.setText(arr[toolstringindex]);
layout.addView(tv);
// // int[] values = new int[2];
float viewx = allTextView.get(placeindex).getX();
float viewy = allTextView.get(placeindex).getY();
// // int x = values[0];
// // int y = values[1];
System.out.println(" hello x::" + x + 50 + "y :: " + y);
Toast toast = new Toast(contextG);
toast.setGravity(Gravity.CENTER_VERTICAL, (int) x, (int) y - 80);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}
现在实际问题是x和y位置不完美 显示在屏幕上的其他地方所以,我没有得到它做什么?如果有的话 一个人有更好的想法,请让我实现这一点。一些 代码行对我有帮助。请帮助我。提前致谢
答案 0 :(得分:0)
改变这个:
LinearLayout.LayoutParams lp;
lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
lp.gravity = Gravity.CENTER;
到此:
LinearLayout.LayoutParams lp;
lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
lp.gravity = Gravity.NO_GRAVITY;
lp.setMargins(x, y, maxX, maxY);
maxX和maxY无关,只需将它们设置为文本宽度和高度。