我在linearlayout内有图像视图。我试图在单击其parent(linearlayout)时获取所有的imageviews(children)。您能告诉我单击线性布局时如何访问线性布局的所有子级吗?
答案 0 :(得分:1)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context=".MainActivity"
android:id="@+id/linerLayout"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView"
android:src="@drawable/ic_launcher_background"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/textId"
/>
</LinearLayout>
package com.example.macchhindra.stackoverflowexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
LinearLayout linearLayout;
TextView myTextView;
ImageView myImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout = (LinearLayout) findViewById(R.id.linerLayout);
myTextView = (TextView) findViewById(R.id.textId);
myImageView = (ImageView) findViewById(R.id.imageView);
linearLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/// change your Image view image in this section
}
});
}
}
答案 1 :(得分:0)
尝试此代码。
LinearLayout ll =findViewById(R.id.linear);
final int childCount = ll.getChildCount();
for (int i = 0; i < childCount; i++) {
View v = ll.getChildAt(i);
}