2016-09-14 58 views
0

我有這樣的自定義視圖:CustomView保持同一高度

public class TopBarHandler extends FrameLayout { 

    private Drawable mRightButtonSrc; 
    private boolean mShowRightButton; 
    private String mTitle; 


    private ImageButton mHamburgerButton; 
    private EllipsizingTextView mTitleTextView; 
    private ImageButton mRightButton; 
    private ProgressBar mProgressBar; 


    public TopBarHandler(final Context context, final AttributeSet attrs) { 
     super(context, attrs); 
     initView(); 
     TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.topbarMenu); 
     mShowRightButton = a.getBoolean(R.styleable.topbarMenu_showRightButton, false); 
     mRightButtonSrc = a.getDrawable(R.styleable.topbarMenu_rightButtonSrc); 
     mTitle = a.getString(R.styleable.topbarMenu_titleText); 
    } 

    public TopBarHandler(final Context context) { 
     super(context); 
     initView(); 
    } 

    private void initView() { 
     View view = inflate(getContext(), R.layout.top_bar_layout, null); 
     mHamburgerButton = (ImageButton) findViewById(R.id.top_bar_hamburger_button); 
     mTitleTextView = (EllipsizingTextView) findViewById(R.id.top_bar_title); 
     mRightButton = (ImageButton) findViewById(R.id.top_bar_right_button); 
     mProgressBar = (ProgressBar) findViewById(R.id.top_bar_progress_bar); 

     addView(view); 
    } 


    public interface TopBarListener { 
     void onHamburgerButtonPressed(); 
     void onRightButtonPressed(); 

    } 
} 

有了這個佈局定義:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/top_bar_layout" 
    android:layout_width="match_parent" 
    android:layout_height="@dimen/height_top_bar" 
    android:background="@drawable/gradient_top_bar" 
    android:gravity="center_vertical"> 

    <ImageButton 
     android:id="@+id/top_bar_hamburger_button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true" 
     android:layout_marginLeft="@dimen/margin_objects_top_bar" 
     android:src="@drawable/selector_menu_topbar" 
     android:background="@null"/> 

    <com.myproject.android.utilities.fonts.EllipsizingTextView 
     android:id="@+id/top_bar_title" 
     style="@style/bar_title_style" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:text="@string/CameraWizard_CameraWizard" 
     android:textSize="@dimen/menu_top_bar_title_size" 
     android:layout_marginLeft="@dimen/margin_top_bar_title" 
     android:layout_toLeftOf="@+id/top_bar_right_button" 
     android:layout_alignParentLeft="true"/> 

    <ImageButton 
     android:id="@+id/top_bar_right_button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     android:layout_marginRight="@dimen/margin_objects_top_bar" 
     android:background="@drawable/top_bar_search" 
     android:visibility="invisible" /> 

    <ProgressBar 
     android:id="@+id/top_bar_progress_bar" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     android:layout_marginRight="@dimen/margin_objects_top_bar" 
     android:indeterminate="false" 
     android:minHeight="50dp" 
     android:minWidth="50dp" 
     android:progress="1" 
     android:visibility="gone" /> 
</RelativeLayout> 

而且我希望把它放在多個佈局,所以我不喜歡這樣寫道:

<com.myproject.android.menu.TopBarHandler 
     android:layout_height="@dimen/height_top_bar" 
     android:layout_width="fill_parent" 
     /> 

但事實是,如果我想把這個多種佈局(70+),那麼我必須指定的每一個的高度和寬度它們。有沒有辦法在自定義視圖中強制執行高度?

如果我不指定目標佈局上的高度和寬度,那麼我得到一個錯誤。如果我把wrap_content的topbar_layout中指定的高度忽略。

+1

我想你錯過了,包括你把多個佈局視圖的方式。請包括這一點! –

+0

@TodorKostov它在那裏,但由於某種原因,它沒有被顯示:P現在。 – MichelReap

+0

我想不出一種可以跳過將寬度和高度屬性添加到視圖並保持相同高度的方式。這些屬性對於視圖是強制的。 –

回答

0

您需要在您的自定義視圖實現onMeasure():

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

    // What is the desired size of the view? 
    int desiredWidth = 250; 
    int desiredHeight = 250; 

    // The MeasureSpecs passed in will depend on the LayoutParams 
    // applied to the view in xml or programmatically 

    // Analyse the MeasureSpec for width and height separately like this: 
    int widthMode = MeasureSpec.getMode(widthMeasureSpec); 
    int widthSize = MeasureSpec.getSize(widthMeasureSpec); 
    switch (widthMode) { 
     case MeasureSpec.UNSPECIFIED: 
      widthSize = desiredWidth; 
      break; 
     case MeasureSpec.AT_MOST: 
      widthSize = Math.min(widthSize, desiredWidth); 
      break; 
     case MeasureSpec.EXACTLY: 
      break; 
     default: 
    } 
    int heightMode = MeasureSpec.getMode(heightMeasureSpec); 
    int heightSize = MeasureSpec.getSize(heightMeasureSpec); 
    switch (heightMode) { 
     case MeasureSpec.UNSPECIFIED: 
      heightSize = desiredHeight; 
      break; 
     case MeasureSpec.AT_MOST: 
      heightSize = Math.min(heightSize, desiredHeight); 
      break; 
     case MeasureSpec.EXACTLY: 
      break; 
     default: 
    } 
    // You must call setMeasuredDimension() 
    setMeasuredDimension(widthSize, heightSize); 
} 
+0

那麼你如何使這樣的寬度匹配父母? – MichelReap

+0

您必須爲佈局中的每個視圖指定layout_height和layout_width。也許我誤解了你的問題。這隻允許給定一個給定LayoutParams的wrap_content的默認大小 –