【问题标题】:android: custom view layout wrappingandroid:自定义视图布局包装
【发布时间】:2015-03-22 16:34:46
【问题描述】:

我想要两个这样的自定义视图:

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 ...
}

LoadingView 有这样的布局:

<RelativeLayout>
    <FrameLayout
        id="@+id/content">
        <!--this is where content goes-->
    </FrameLayout>

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

这样任何继承自它的自定义视图都将被注入FrameLayout

所以如果OtherView有自己的布局,它会自动嵌套在FrameLayout里面,你就可以调用myOtherView.setIsLoading(true/false)

你会如何建议这样做?

【问题讨论】:

  • 您可以在 public View onCreateContentView() 内添加一个方法 LoadingView - 默认值:返回 null 。让子类覆盖这个方法并返回一些有意义的东西。

标签: android android-layout android-fragments android-activity android-custom-view


【解决方案1】:

在膨胀第一个视图时保留对内容 FrameLayout 的引用。

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>

other_view.xml

<?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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-16
    • 2013-11-06
    • 2013-07-24
    • 1970-01-01
    • 2011-07-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多