这是我的代码:
package mika.actual;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class accordion extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.accordion);
final View panels[] = {
findViewById(R.id.panelProfile),
findViewById(R.id.panelSettings),
findViewById(R.id.panelPrivacy)
};
final Button buttons[] = {
(Button) findViewById(R.id.btnProfile),
(Button) findViewById(R.id.btnSettings),
(Button) findViewById(R.id.btnPrivacy)
};
for (int i = 0; i < panels.length; i++){
panels[i].setVisibility(View.GONE);
}
for (int i = 0; i < buttons.length; i++){
final int x = i;
buttons[i].setBackgroundColor(getResources().getColor(R.color.button_not_pressed));
buttons[i].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
for (int y = 0; y < panels.length; y++){
panels[y].setVisibility(View.GONE);
}
panels[x].setVisibility(View.VISIBLE);
for (int y = 0; y < buttons.length; y++){
buttons[y].setBackgroundColor(getResources().getColor(R.color.button_not_pressed));
}
buttons[x].setBackgroundColor(getResources().getColor(R.color.button_pressed));
}
});
}
}
}
和XML:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFFFF"
android:orientation="vertical">
<Button
android:id="@+id/btnProfile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Profile"
android:textColor="#FFFFFFFF" />
<LinearLayout
android:id="@+id/panelProfile"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FFFFFFFF">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFFFF">
<LinearLayout
android:id="@+id/panelProfile1"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF">
<TextView
android:id="@+id/strName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name" />
<EditText
android:id="@+id/txtName"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="@+id/panelProfile2"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF">
<TextView
android:id="@+id/strSurname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Surname" />
<EditText
android:id="@+id/txtSurname"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
<Button
android:id="@+id/btnSettings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Settings"
android:textColor="#FFFFFFFF" />
<LinearLayout
android:id="@+id/panelSettings"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FFFFFFFF">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="@+id/panelSettings1"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF">
<TextView
android:id="@+id/strMail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="e-mail" />
<EditText
android:id="@+id/txtMail"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="@+id/panelSettings2"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF">
<TextView
android:id="@+id/strPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phone" />
<EditText
android:id="@+id/txtPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
<Button
android:id="@+id/btnPrivacy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Privacy"
android:textColor="#FFFFFFFF" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/panelPrivacy"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FFFFFFFF">
<CheckBox
android:id="@+id/checkFacebook"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Facebook"
android:textColor="#ff355689">
</CheckBox>
<CheckBox
android:id="@+id/checkLinkedIn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="LinkedIn"
android:textColor="#ff355689">
</CheckBox>
<CheckBox
android:id="@+id/checkTwitter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Twitter"
android:textColor="#ff355689">
</CheckBox>
</LinearLayout>
</ScrollView>
</LinearLayout>
我想动态创建两个数组buttons []和panels [],获取LinarLayout中的所有子视图(id为root_layout)。任何建议赞赏。在此先感谢:)
答案 0 :(得分:0)
也许迭代你的Viewgroup的孩子并将他们添加到Arraylist中?:
LinearLayout rootView = (LinearLayout) findViewById(R.id.mylinearlayoutid);
ArrayList<Button> buttons = new ArrayList<>(rootView.getChildCount());
for(int i = 0; i < rootView.getChildCount(); i++){
buttons.add ((Button) rootView.getChildAt(i));
}