2015-04-07 32 views
2

我創建了自定義視圖,它擴展了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> 

回答

0

即使我面臨同樣的問題,看來機器人工作室的新版本又發表了兩個文件content_main.xml和activity_mail.xml,當我們選擇Activity_main .xml>設計視圖一切都會出現'自定義視圖',而當我們突出顯示content_main.xml>設計時,一切都是正常的。我不知道它爲什麼會發生,但那是我如何修復我的(機器人nooob在這裏)

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