1

我有一个有苹果、梨和柠檬的东西。我为苹果、梨和柠檬做了单独的布局。

如果选择了任何元素,我想获取该元素的布局。我尝试根据所选布局和inflate()功能将正确的布局放置到 viewStub 中。inflate()但是每次定义布局的性能都令人筋疲力尽,有时还会导致错误的工作。

请帮我。

我的一般视图代码:

    public class MyView extends RelativeLayout  {
    private ViewStub viewStub;
    private int lemon = 1, apple = 2, pear = 3;

    public MyView(Context context) {
        this(context, null);
    }

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

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        this.viewStub = ViewUtil.findById(this, R.id.view_stub);
    }

    public void bindElements(int type){
    if (type == lemon) {castLemon(); return;} 
    if (type == apple ) {castApple(); return;} 
    if (type == pear) {castPear(); return;} 

     public void castLemon() {

            viewStub.setLayoutResource(R.layout.lemon_item);
            LemonView lemonView = (LemonView) viewStub.inflate();
        }
     }

    public void castApple() {

            viewStub.setLayoutResource(R.layout.apple_item);
            AppleView appleView = (AppleView) viewStub.inflate();
        }
     }

    public void castPear() {

            viewStub.setLayoutResource(R.layout.pear_item);
            PearView pearView = (PearView) viewStub.inflate();
        }
     }
  }

我的一般视图xml:

<com.android.android.MyView
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/myView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <LinearLayout android:id="@+id/bodyLinear"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

                       <ViewStub
                        android:id="@+id/view_stub"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="start|center_vertical"
                        android:gravity="start|center_vertical"
                        android:paddingBottom="5dp"/>

            </LinearLayout>

</com.android.android.MyView>

柠檬视图.java:

public class LemonView extends FrameLayout {

    private ImageView imageView;

    public LemonView(Context context) {
        this(context, null);
    }

    public LemonView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LemonView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        inflate(context, R.layout.lemon_view, this);

        imageView = findViewById(R.id.image);
    }

柠檬视图:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.android.android.LemonView">

    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:adjustViewBounds="true"
        android:contentDescription="@null"
        android:gravity="center"
        android:scaleType="centerCrop"/>
</merge>

柠檬项目 xml:

<?xml version="1.0" encoding="utf-8"?>
<com.android.android.LemonView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/lemon_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
4

1 回答 1

1

.setVisibility(View.VISIBLE)一次创建所有布局,然后使用/显示/隐藏所需的布局变体.setVisibility(View.GONE)

于 2018-02-22T15:16:01.120 回答