gpt4 book ai didi

android - 如何获取线性布局的子位置

转载 作者:行者123 更新时间:2023-11-29 15:09:34 25 4
gpt4 key购买 nike

我有一个 LinearLayout,它包含几个子 ImageView(动态添加 ImageViews 到 LinearLayout Horizo​​ntally)。长按 subview (ImageView)时,如何获取该 LinerLayout 的 subview 的位置?

添加了setTag:

 image.setImageBitmap(resizedBitmap);
image.setTag(fullUrl);



imageViewLinearLayout.setOnLongClickListener(new View.OnLongClickListener(){
@Override
public boolean onLongClick(View v) {

Log.d("Mobi","Path :"+v.getTag());

return false;
}
});

添加了 set Tag as file path but return v.getTag() as null"?

最佳答案

添加这个test.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<Button
android:id="@+id/btnAddImage"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add Image" />

<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1.5"
android:fadingEdge="none"
android:orientation="horizontal" >

<LinearLayout
android:id="@+id/lnrParent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>

</TableLayout>

和另一个布局lay_image.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lnrChild"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<Button
android:id="@+id/btnExample"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />

<ImageView
android:id="@+id/imgView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50sp"
android:src="@drawable/ic_launcher" />

</LinearLayout>

在这里编码

setContentView(R.layout.test);

final LinearLayout LnrLayout = (LinearLayout) findViewById(R.id.lnrParent);

Button BtnAddImage = (Button) findViewById(R.id.btnAddImage);

BtnAddImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final LinearLayout LnrChildLayout = (LinearLayout) getLayoutInflater()
.inflate(R.layout.lay_image, null);
LnrChildLayout.setLayoutParams(new LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));

LnrLayout.addView(LnrChildLayout);

int indexValue = LnrLayout.indexOfChild(LnrChildLayout);
LnrChildLayout.setTag(Integer.toString(indexValue));

final ImageView ImgView = (ImageView) LnrChildLayout
.findViewById(R.id.imgView);

int ImagePosition = LnrChildLayout.indexOfChild(ImgView);
ImgView.setTag(Integer.toString(ImagePosition));

ImgView.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {

// get Position
String strPosition = (String) v.getTag();
Toast.makeText(test.this,
"ImageView getTag : >" + strPosition,
Toast.LENGTH_SHORT).show();
// getId
int strId = v.getId();
Toast.makeText(test.this,
"ImageView getId : >" + strId,
Toast.LENGTH_SHORT).show();

return true;

}
});

LnrChildLayout
.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {

String strPosition = (String) v.getTag();
Toast.makeText(test.this, "" + strPosition,
Toast.LENGTH_SHORT).show();

return true;
}
});

Button BtnExample = (Button) LnrChildLayout
.findViewById(R.id.btnExample);
int ButtonPosition = LnrChildLayout.indexOfChild(BtnExample);
BtnExample.setTag(Integer.toString(ButtonPosition));

BtnExample.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {

String strPosition = (String) v.getTag();
Toast.makeText(test.this,
"Button getTag : >" + strPosition,
Toast.LENGTH_SHORT).show();

int strId = v.getId();
Toast.makeText(test.this, "Button getId : >" + strId,
Toast.LENGTH_SHORT).show();

return true;
}
});

}
});

关于android - 如何获取线性布局的子位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33141637/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com