Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android:自定义视图布局包装_Android_Android Layout_Android Fragments_Android Activity_Android Custom View - Fatal编程技术网

android:自定义视图布局包装

android:自定义视图布局包装,android,android-layout,android-fragments,android-activity,android-custom-view,Android,Android Layout,Android Fragments,Android Activity,Android Custom View,我希望有两个这样的自定义视图: class LoadingView extends RelativeLayout{ //this view has a progressBar and can show and hide it. void setIsLoading(){ // shows/hides progress bar } // some code ... } class OtherView extends LoadingView{

我希望有两个这样的自定义视图:

class LoadingView extends RelativeLayout{
    //this view has a progressBar and can show and hide it.

    void setIsLoading(){
        // shows/hides progress bar
    }


    // some code ...
}

class OtherView extends LoadingView{
    //some code ...
}
<RelativeLayout>
    <FrameLayout
        id="@+id/content">
        <!--this is where content goes-->
    </FrameLayout>

    <ProgressBar
        id="@+id/pb"/>
</RelativeLayout>
LoadingView的布局如下所示:

class LoadingView extends RelativeLayout{
    //this view has a progressBar and can show and hide it.

    void setIsLoading(){
        // shows/hides progress bar
    }


    // some code ...
}

class OtherView extends LoadingView{
    //some code ...
}
<RelativeLayout>
    <FrameLayout
        id="@+id/content">
        <!--this is where content goes-->
    </FrameLayout>

    <ProgressBar
        id="@+id/pb"/>
</RelativeLayout>

因此,从中继承的任何自定义视图都将被注入到
FrameLayout

因此,如果
OtherView
有自己的布局,它将自动嵌套在
FrameLayout
中,您将能够调用
myOtherView.setisload(真/假)


您建议如何执行此操作?

在展开第一个视图时,请保留对内容
框架布局的引用

public class LoadingView extends RelativeLayout {

    private ProgressBar progressBar;
    private FrameLayout contentFrame;

    public LoadingView(Context context) {
        super(context);
        initialize();
    }

    public LoadingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize();
    }

    public LoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initialize();
    }

    private void initialize() {
        View root = inflate(getContext(), R.layout.loading_view, this);
        progressBar = (ProgressBar) root.findViewById(R.id.progress_bar);
        contentFrame = (FrameLayout) root.findViewById(R.id.content_frame);
    }

    public void setLoading(boolean loading) {
        progressBar.setVisibility(loading ? VISIBLE : GONE);
    }

    protected FrameLayout getContentFrame() {
        return contentFrame;
    }

}
然后在展开子视图时,使用
getContentFrame
作为父视图

public class OtherView extends LoadingView {

    public OtherView(Context context) {
        super(context);
        initialize();
    }

    public OtherView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize();
    }

    public OtherView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initialize();
    }

    private void initialize() {
        View root = inflate(getContext(), R.layout.other_view, getContentFrame());
    }

}
loading_view.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <ProgressBar
        android:id="@+id/progress_bar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical">

    <TextView
        android:text="Title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <TextView
        android:text="Subtitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>
像这样使用它

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <com.example.client.ui.OtherView
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</FrameLayout>

您可以在
加载视图
中添加一个方法
public View onCreateContentView()
-默认值:返回null。让子类重写此方法并返回有意义的内容。