Java 如何访问linearLayout的所有子级?
我在线性布局中有图像视图。我试图在单击其父对象(linearlayout)时获取所有ImageView(子对象)。您能告诉我单击linearlayout时如何访问linearlayout的所有子项吗 试试这段代码Java 如何访问linearLayout的所有子级?,java,android,android-studio,android-layout,imageview,Java,Android,Android Studio,Android Layout,Imageview,我在线性布局中有图像视图。我试图在单击其父对象(linearlayout)时获取所有ImageView(子对象)。您能告诉我单击linearlayout时如何访问linearlayout的所有子项吗 试试这段代码 LinearLayout ll =findViewById(R.id.linear); final int childCount = ll.getChildCount(); for (int i = 0; i < childCount; i++) {
LinearLayout ll =findViewById(R.id.linear);
final int childCount = ll.getChildCount();
for (int i = 0; i < childCount; i++) {
View v = ll.getChildAt(i);
}
LinearLayout ll=findviewbyd(R.id.linear);
final int childCount=ll.getChildCount();
for(int i=0;i
请详细说明您想要实现的目标。看起来您在询问之前并没有搜索过,但您必须这样做。
<?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
}
});
}
}