我创建了自己的XML元素,它应该占用所有子LinearLayouts,为每个子元素创建一个按钮,并将每个元素放入一个新的ScrollView中。当我完成步骤的代码步骤时,它似乎对我有效,除了它实际上没有给出真正的输出。 (包含截图)我没有任何错误。
package mika.actual;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import java.util.ArrayList;
public class AccordionWidget extends LinearLayout{
AttributeSet xattrs;
Context xcontext;
public AccordionWidget(Context context, AttributeSet attrs) {
super(context, attrs);
xattrs = attrs;
xcontext = context;
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int count = this.getChildCount();
AttributeSet attrs = xattrs;
Context context = xcontext;
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.accordion);
String[] btns = getResources().getStringArray(R.array.buttons);
ArrayList<LinearLayout> lls = new ArrayList <> (count);
for(int i = 0; i < count; i++){
View child = this.getChildAt(i);
if (child instanceof LinearLayout) {
lls.add((LinearLayout)child);
}
}
for(int j = 0; j < lls.size(); j++){
if(btns[j] == null){
Log.e("error: ", "Please add more Button lables to the strings.xml file.");
break;
}
final Button btn = new Button(context);
final LinearLayout ll = lls.get(j);
final ScrollView sv = new ScrollView(context);
final int col_pressed = a.getColor(R.styleable.accordion_backgroundColorPressed, Color.BLACK);
final int col_unpressed = a.getColor(R.styleable.accordion_backgroundColorUnpressed, Color.YELLOW); //Black and yellow, black and yellow...
LayoutParams btnparams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
LayoutParams swparams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
LayoutParams llparams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
btn.setText(btns[j]);
btn.setBackgroundColor(col_pressed);
llparams.weight = 1f;
btn.setLayoutParams(btnparams);
ll.setLayoutParams(llparams);
sv.setLayoutParams(swparams);
ll.setOrientation(LinearLayout.VERTICAL);
sv.setVisibility(GONE);
this.removeView(ll);
sv.addView(ll);
this.addView(sv);
this.addView(btn);
btn.setOnClickListener(new OnClickListener() {
private boolean btnstate = false;
@Override
public void onClick(View v) {
if (btnstate) {
btn.setBackgroundColor(col_pressed);
sv.setVisibility(VISIBLE);
btnstate = true;
} else {
btn.setBackgroundColor(col_unpressed);
sv.setVisibility(GONE);
btnstate = false;
}
}
});
}
a.recycle();
}
}
<?xml version="1.0" encoding="utf-8"?>
<mika.actual.AccordionWidget
xmlns:accordion="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
accordion:text_color="@color/text_color"
accordion:backgroundColorPressed="@color/button_pressed"
accordion:backgroundColorUnpressed="@color/button_not_pressed"
android:id="@+id/swaggy"
android:layout_height="match_parent"
android:layout_width="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="yolooooo yooo"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="yolooooo yooo"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="yolooooo yooo"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="yolooooo yooo"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="yolooooo yooo"/>
</LinearLayout>
</mika.actual.AccordionWidget>
答案 0 :(得分:0)
我没有使用match_parent
而是使用wrap_content
作为scrollView,并将ScrollView的权重设置为我正在寻找的结果的权重。我还添加了一些ArrayLists并将所有Buttons和ScrollViews放在里面,以便在打开另一个时打开所有打开的面板,所以一次只打开一个。
package mika.actual;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import java.util.ArrayList;
public class AccordionWidget extends LinearLayout{
ArrayList<Button> btnViews = new ArrayList <> ();
ArrayList<ScrollView> svViews = new ArrayList <> ();
AttributeSet xattrs;
Context xcontext;
public AccordionWidget(Context context, AttributeSet attrs) {
super(context, attrs);
xattrs = attrs;
xcontext = context;
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
final int count = this.getChildCount();
AttributeSet attrs = xattrs;
Context context = xcontext;
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.accordion);
String[] btns = getResources().getStringArray(R.array.button_lables);
ArrayList<LinearLayout> lls = new ArrayList <> (count);
for(int i = 0; i < count; i++){
View child = this.getChildAt(i);
if (child instanceof LinearLayout) {
lls.add((LinearLayout)child);
}
}
for(int j = 0; j < lls.size(); j++){
if(btns[j] == null){
throw new NullPointerException("Needs more Button lables in strings.xml");
}
final Button btn = new Button(context);
final LinearLayout ll = lls.get(j);
final ScrollView sv = new ScrollView(context);
final int col_pressed = a.getColor(R.styleable.accordion_buttonColorPressed, Color.BLACK);
final int col_unpressed = a.getColor(R.styleable.accordion_buttonColorUnpressed, Color.YELLOW); //Black and yellow, black and yellow...
LayoutParams btnparams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
LayoutParams svparams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
LayoutParams llparams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
btn.setText(btns[j]);
btn.setBackgroundColor(col_unpressed);
svparams.weight = 1f;
btn.setLayoutParams(btnparams);
ll.setLayoutParams(llparams);
sv.setLayoutParams(svparams);
ll.setOrientation(LinearLayout.VERTICAL);
sv.setVisibility(GONE);
this.removeView(ll);
sv.addView(ll);
this.addView(btn);
this.addView(sv);
btnViews.add(btn);
svViews.add(sv);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int visibility = sv.getVisibility();
for (int k = 0; k < btnViews.size(); k++){
btnViews.get(k).setBackgroundColor(col_unpressed);
svViews.get(k).setVisibility(GONE);
}
if (visibility == VISIBLE) {
btn.setBackgroundColor(col_unpressed);
sv.setVisibility(GONE);
} else {
btn.setBackgroundColor(col_pressed);
sv.setVisibility(VISIBLE);
}
}
});
}
a.recycle();
}
}