Я создал динамическую кнопку и добавил ее в LinearLayout, родительский вид линейного макета - HorizontalScrollView. Мне нужно динамически перемещать положение прокрутки Horozontal scollview.Расположение кнопки на экране
Я не могу получить местоположение кнопки на экране. Он всегда дает [0,0] для кнопки Y и Y Location на экране.
Мой XML-файл:
<HorizontalScrollView
android:id="@+id/horizontalScrollView1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_above="@id/imv_Line_Invisibe"
android:layout_marginLeft="15dp"
android:layout_toRightOf="@id/imv_logo"
android:scrollbars="none" >
<LinearLayout
android:id="@+id/lay_but"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
Добавление кнопки динамически в линейной компоновке:
int i = 0;
for (HashMap<String, String> map : category)
for (Entry<String, String> mapEntry : map.entrySet()) {
String strButLabel = mapEntry.getValue();
Log.i("map", "" + strButLabel);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
params.weight = 1.0f;
String value = mapEntry.getValue();
TextView tvSetID = new TextView(this);
tvSetID.setId(i);
butCategory = new Button(this);
butCategory.setId(i);
butCategory.setText(value);
butCategory.setTextSize(13);
butCategory.setAllCaps(true);
butCategory.setLayoutParams(params);
butCategory.setTypeface(custom_font, Typeface.BOLD);
butCategory.setGravity(Gravity.CENTER | Gravity.BOTTOM);
butCategory.setPadding(0, 0, 20, 20);
butCategory.setBackgroundResource(R.drawable.nav_phone_unselect);
butCategory.setTextColor(Color.parseColor("#666666"));
butCategory.setOnClickListener(OnClickChangeButtonColor(butCategory));
i++;
}
Это код, я использую для получения расположение кнопки на экране:
int temp_index = intent.getIntExtra("btn_index_value", 0);
Button butCategory = (Button) findViewById(temp_index);
butCategory.setBackgroundResource(R.drawable.nav_phone_select);
butCategory.setTextColor(Color.parseColor("#FFFFFF"));
String i1 = butCategory.getText().toString();
int[] locations = new int[2];
butCategory.getLocationOnScreen(locations);
int x = locations[0];
int y = locations[1];
horizontal_Category.scrollTo(x, y);
index = temp_index;
Вы можете использовать View.getX() и View.getY(), чтобы получить координаты x и y относительно пикселей. – Muthu