自定义视图在组件树中显示为嵌套视图

时间:2015-04-07 10:36:03

标签: android android-studio

我创建了扩展FrameLayout的自定义视图。将它添加到RelativeLayout后,它将显示为两个嵌套视图:

enter image description here

这是正常的吗?它有时会与wrap_content标志混淆,但我无法弄清楚为什么。当我使用View作为基类时,一切看起来都很正常。

这是我的代码:

MainActivity.java

package com.example.app;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SeekBar;

import com.codersmill.tset.R;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

}

activity_main.xml中

<RelativeLayout 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"
                tools:context=".MainActivity">

    <com.example.app.RateBar
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:id="@+id/rateBar"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>

</RelativeLayout>

RateBar.java

package com.example.app;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.codersmill.tset.R;


public class RateBar extends FrameLayout {

    TextView star1, star2, star3, star4, star5;
    View dot;

    private boolean isAnimating = false;

    int radius = 36;
    private int step = 100;
    private int leftMargin = 50;
    private int topMargin = 0;

    private int currentPosition = 0;

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

    public RateBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        View.inflate(context, R.layout.view_rate_bar, this);

        star1 = (TextView) this.findViewById(R.id.star1);
        star2 = (TextView) this.findViewById(R.id.star2);
        star3 = (TextView) this.findViewById(R.id.star3);
        star4 = (TextView) this.findViewById(R.id.star4);
        star5 = (TextView) this.findViewById(R.id.star5);

        star1.setOnClickListener(onClickListener);
        star2.setOnClickListener(onClickListener);
        star3.setOnClickListener(onClickListener);
        star4.setOnClickListener(onClickListener);
        star5.setOnClickListener(onClickListener);

        dot = this.findViewById(R.id.selector);

    }

    public RateBar(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs);
    }

    private OnClickListener onClickListener = new OnClickListener() {
        @Override public void onClick(View v) {
            if(isAnimating) return;
            switch (v.getId()) {
                case R.id.star1:
                    animateToPosition(0);
                    break;
                case R.id.star2:
                    animateToPosition(1);
                    break;
                case R.id.star3:
                    animateToPosition(2);
                    break;
                case R.id.star4:
                    animateToPosition(3);
                    break;
                case R.id.star5:
                    animateToPosition(4);
                    break;
            }
        }
    };

    @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        step = (int) (getMeasuredWidth() / 5.0f);
        leftMargin = (int) (step / 2.0f - radius);
        topMargin = (int) (getMeasuredHeight() / 2.0f - radius);

        if(!isAnimating) {
            FrameLayout.LayoutParams params = (LayoutParams) dot.getLayoutParams();
            params.leftMargin = leftMargin + currentPosition * step;
            params.topMargin = topMargin;
            dot.setLayoutParams(params);
        }

    }

    private void animateToPosition(final int position) {

        final int from = currentPosition*step + leftMargin;
        final int to = position*step + leftMargin;

        ValueAnimator animation = ValueAnimator.ofInt(from, to);
        animation.setDuration(250);
        animation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override public void onAnimationUpdate(ValueAnimator animation) {
                FrameLayout.LayoutParams params = (LayoutParams) dot.getLayoutParams();
                params.leftMargin = (int) animation.getAnimatedValue();
                params.topMargin = topMargin;
                dot.setLayoutParams(params);
            }
        });
        animation.addListener(new AnimatorListenerAdapter() {
            @Override public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
                isAnimating = true;
                FrameLayout.LayoutParams params = (LayoutParams) dot.getLayoutParams();
                params.topMargin = topMargin;
                params.leftMargin = currentPosition * step + leftMargin;
                dot.setLayoutParams(params);
            }

            @Override public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                isAnimating = false;
                currentPosition = position;
                FrameLayout.LayoutParams params = (LayoutParams) dot.getLayoutParams();
                params.leftMargin = currentPosition * step + leftMargin;
                params.topMargin = topMargin;
                dot.setLayoutParams(params);
            }
        });

        animation.start();

    }

    private void updateDotPosition() {

    }


}

view_rate_bar.xml

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

    <View
        android:id="@+id/selector"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:background="@drawable/oval"
        />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="1"
            android:id="@+id/star1"
            android:layout_weight="1"
            android:gravity="center"
            android:padding="12dp"
            android:background="#2200ff00"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="2"
            android:id="@+id/star2"
            android:layout_weight="1"
            android:gravity="center"
            android:padding="12dp"
            android:background="#22ff0000"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="3"
            android:id="@+id/star3"
            android:layout_weight="1"
            android:gravity="center"
            android:padding="12dp"
            android:background="#2200ff00"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="4"
            android:id="@+id/star4"
            android:layout_weight="1"
            android:gravity="center"
            android:padding="12dp"
            android:background="#22ff0000"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="5"
            android:id="@+id/star5"
            android:layout_weight="1"
            android:gravity="center"
            android:padding="12dp"
            android:background="#2200ff00" />
    </LinearLayout>

</merge>

1 个答案:

答案 0 :(得分:0)

即使我面临同样的问题,似乎新版本的android studio提供了两个文件content_main.xml和activity_mail.xml,当我们选择Activity_main.xml&gt; Design视图时,所有内容都显示为'Custom View',而不是我们重点介绍content_main.xml&gt;设计,一切都正常。我不知道为什么会发生这种情况;我是如何修复我的(这里是android nooob)

更多信息可以在这里找到:https://teamtreehouse.com/community/i-cant-drag-widgets-onto-the-phone-mockup-component-tree-shows-customview-instead-of-the-relative-view-help