如何访问所有 children 的 linearLayout?
How to access all children of linearLayout?
我在线性布局中有图像视图。单击 parent(linearlayout) 时,我试图获取所有 imageviews(children)。您能告诉我如何在单击线性布局时访问线性布局的所有 children 吗?
试试这个代码..
LinearLayout ll =findViewById(R.id.linear);
final int childCount = ll.getChildCount();
for (int i = 0; i < childCount; i++) {
View v = ll.getChildAt(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>
MainActivity.java
package com.example.macchhindra.Whosebugexample;
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
}
});
}
}
我在线性布局中有图像视图。单击 parent(linearlayout) 时,我试图获取所有 imageviews(children)。您能告诉我如何在单击线性布局时访问线性布局的所有 children 吗?
试试这个代码..
LinearLayout ll =findViewById(R.id.linear);
final int childCount = ll.getChildCount();
for (int i = 0; i < childCount; i++) {
View v = ll.getChildAt(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>
MainActivity.java
package com.example.macchhindra.Whosebugexample;
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
}
});
}
}