我有這樣的自定義視圖: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中指定的高度忽略。
我想你錯過了,包括你把多個佈局視圖的方式。請包括這一點! –
@TodorKostov它在那裏,但由於某種原因,它沒有被顯示:P現在。 – MichelReap
我想不出一種可以跳過將寬度和高度屬性添加到視圖並保持相同高度的方式。這些屬性對於視圖是強制的。 –