0

我面临一个问题。

我创建了一个自定义布局。

我需要在单击可扩展布局时展开和折叠这些子视图。我如何为这个示例应用程序中的所有可扩展视图执行此操作。

我只想在可扩展视图中添加子项,不能使用活动来设置子项,所有内容都将在我的 ExpandableView 类中。

//带有自定义布局的myactivity布局

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res/com.example.customview"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <com.example.customview.ExpandableView
            android:id="@+id/ex_view1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            custom:buttonTitle="Details"
            custom:title="All staff Details"
            custom:visibility="@integer/visible" 
            android:orientation="vertical">
        </com.example.customview.ExpandableView>

        <com.example.customview.ExpandableView
            android:id="@+id/ex_view2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            custom:title="Next coming"
            custom:visibility="@integer/gone" />

        <com.example.customview.ExpandableView
            android:id="@+id/ex_view3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            custom:title="Previous record"
            custom:visibility="@integer/gone" />

        <com.example.customview.ExpandableView
            android:id="@+id/ex_view4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            custom:buttonTitle="Next"
            custom:title="New News"
            custom:visibility="@integer/visible" />
    </LinearLayout>

</ScrollView>

// 主布局

<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"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/parent_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#AAABAB"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/tv_text"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1"
            android:paddingBottom="15dp"
            android:paddingTop="15dp"
            android:textSize="18sp"
            android:textStyle="bold" />

        <Button
            android:id="@+id/btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:background="#B7B7B8"
            android:drawableLeft="@drawable/ic_menu_edit"
            android:paddingRight="5dp" />

    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="5dp" />

</LinearLayout>

//代码

public class ExpandableView extends LinearLayout 
{
    TextView textView;
    Button button;
    LinearLayout parentLinear,childLinear;
    View view,convertView;
    Context context;
    WebView web;
    LayoutInflater inflater ;
    View viewLay ; 

    /**
     * Constructor
     * @param context
     * @param attr
     */
    public ExpandableView(final Context context,AttributeSet attr) 
    {
        super(context,attr);
        this.context = context;
        inflate(context,R.layout.mainlayout, this);
        textView =(TextView)findViewById(R.id.tv_text);
        button = (Button)findViewById(R.id.btn);
        parentLinear =(LinearLayout)findViewById(R.id.parent_id);
        getAttributes(attr);
        //setChildView();

}

    public void setChildView()
    {
        web = (WebView)findViewById(R.id.webview);
        web.loadUrl("file:///android_asset/info.html");
    }

    /**
     * function to get attributes from attrs.xml
     * @param attr
     */
    public void getAttributes(AttributeSet attr)
    {
        TypedArray attributes = context.obtainStyledAttributes(attr,R.styleable.MyCustomWidget);

        String textTitle = attributes.getString(R.styleable.MyCustomWidget_title);
        textView.setText(textTitle);

        String textButtonTitle = attributes.getString(R.styleable.MyCustomWidget_buttonTitle);
        button.setText(textButtonTitle);

        int visibility = attributes.getInteger(R.styleable.MyCustomWidget_visibility, 0);
        button.setVisibility(visibility);

        attributes.recycle();
    }

}

现在我需要在里面添加一个网络视图custom layout,所以我这样做

 <com.example.customview.ExpandableView
            android:id="@+id/ex_view1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            custom:buttonTitle="Details"
            custom:title="All staff Details"
            custom:visibility="@integer/visible" 
            android:orientation="vertical">

         <WebView
                android:id="@+id/webview"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        </com.example.customview.ExpandableView>

问题案例1:-

如果我在活动中设置这个子 webview,它工作正常。即

//活动类

expandableOne = (ExpandableView)findViewById(R.id.ex_view1);

        expandableOne.setChildView();

但我不想让这个孩子参加活动。

案例2:- 我想在ExpandableView课堂上设置这个

所以我在构造函数中调用这个方法。

setChildview();//这是在构造函数中

expandableOne.setChildView();activity课堂上删除它....现在它崩溃了

我的日志猫

05-03 11:07:29.364: E/AndroidRuntime(1336): FATAL EXCEPTION: main
05-03 11:07:29.364: E/AndroidRuntime(1336): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.customview/com.example.customview.ExpandableActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class com.example.customview.ExpandableView
05-03 11:07:29.364: E/AndroidRuntime(1336):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-03 11:07:29.364: E/AndroidRuntime(1336):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-03 11:07:29.364: E/AndroidRuntime(1336):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-03 11:07:29.364: E/AndroidRuntime(1336):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-03 11:07:29.364: E/AndroidRuntime(1336):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-03 11:07:29.364: E/AndroidRuntime(1336):     at android.os.Looper.loop(Looper.java:137)
05-03 11:07:29.364: E/AndroidRuntime(1336):     at android.app.ActivityThread.main(ActivityThread.java:4424)
05-03 11:07:29.364: E/AndroidRuntime(1336):     at java.lang.reflect.Method.invokeNative(Native Method)
05-03 11:07:29.364: E/AndroidRuntime(1336):     at java.lang.reflect.Method.invoke(Method.java:511)
05-03 11:07:29.364: E/AndroidRuntime(1336):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-03 11:07:29.364: E/AndroidRuntime(1336):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-03 11:07:29.364: E/AndroidRuntime(1336):     at dalvik.system.NativeStart.main(Native Method)
05-03 11:07:29.364: E/AndroidRuntime(1336): Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class com.example.customview.ExpandableView
05-03 11:07:29.364: E/AndroidRuntime(1336):     at android.view.LayoutInflater.createView(LayoutInflater.java:606)
05-03 11:07:29.364: E/AndroidRuntime(1336):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
05-03 11:07:29.364: E/AndroidRuntime(1336):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
05-03 11:07:29.364: E/AndroidRuntime(1336):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
05-03 11:07:29.364: E/AndroidRuntime(1336):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
05-03 11:07:29.364: E/AndroidRuntime(1336):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
05-03 11:07:29.364: E/AndroidRuntime(1336):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
05-03 11:07:29.364: E/AndroidRuntime(1336):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
05-03 11:07:29.364: E/AndroidRuntime(1336):     at android.app.Activity.setContentView(Activity.java:1835)
05-03 11:07:29.364: E/AndroidRuntime(1336):     at com.example.customview.ExpandableActivity.onCreate(ExpandableActivity.java:23)
05-03 11:07:29.364: E/AndroidRuntime(1336):     at android.app.Activity.performCreate(Activity.java:4465)
05-03 11:07:29.364: E/AndroidRuntime(1336):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-03 11:07:29.364: E/AndroidRuntime(1336):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-03 11:07:29.364: E/AndroidRuntime(1336):     ... 11 more
05-03 11:07:29.364: E/AndroidRuntime(1336): Caused by: java.lang.reflect.InvocationTargetException
05-03 11:07:29.364: E/AndroidRuntime(1336):     at java.lang.reflect.Constructor.constructNative(Native Method)
05-03 11:07:29.364: E/AndroidRuntime(1336):     at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
05-03 11:07:29.364: E/AndroidRuntime(1336):     at android.view.LayoutInflater.createView(LayoutInflater.java:586)
05-03 11:07:29.364: E/AndroidRuntime(1336):     ... 23 more
05-03 11:07:29.364: E/AndroidRuntime(1336): Caused by: java.lang.NullPointerException
05-03 11:07:29.364: E/AndroidRuntime(1336):     at com.example.customview.ExpandableView.setChildView(ExpandableView.java:48)
05-03 11:07:29.364: E/AndroidRuntime(1336):     at com.example.customview.ExpandableView.<init>(ExpandableView.java:41)
05-03 11:07:29.364: E/AndroidRuntime(1336):     ... 26 more

/////

案例3:-

4

0 回答 0