我有一个自定义RelativeLayout,可以调整大小到所需的比例。我已经覆盖了看起来像这个
的onMeasure方法@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
float height = getMeasuredHeight();
float mWidth = height / 711.0f * 320.0f;
setMeasuredDimension((int) mWidth, (int)height );
}
这就像我希望它调整视图宽度一样。
我有一个我已经扩展的按钮,以便我可以根据其宽度调整其高度。我用以下代码覆盖了这个自定义类的onMeasure方法。
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
float width = getMeasuredWidth();
float mheight = (width / 320.0f) * 60.0f;
Log.i(TAG, "measures width is "+width + " measured height is "+mheight);
setMeasuredDimension((int) width, (int) mheight);
}
我有一个xml文件,如下所示
<?xml version="1.0" encoding="utf-8"?>
<com.breadcreative.Presenter.Core.SlidingRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@color/pdarkgrey"
android:orientation="vertical"
android:visibility="visible"
android:minWidth="328dp">
<com.breadcreative.Presenter.Core.SquareImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/imageView"
android:src="@drawable/sidebar_logo"
android:maxWidth="300dp" />
<TextView
android:id="@+id/PresentationView"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="PRESENTATION"
android:autoText="false"
android:textColor="#ffffff"
android:gravity="center_vertical"
android:paddingLeft="32dp"
android:textSize="15sp"
android:layout_below="@+id/imageView" />
<com.breadcreative.Presenter.Core.SlidingMenuButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Library"
android:id="@+id/pLibButton"
android:background="@drawable/sidebar_plib"
android:textSize="17dp"
android:textColor="@color/white"
android:gravity="center_vertical|left"
android:clickable="true"
android:onClick="hidePanel"
android:layout_below="@+id/PresentationView"
android:paddingLeft="80dp" />
<com.breadcreative.Presenter.Core.SlidingMenuButton
android:layout_width="wrap_content"
android:layout_height="60dp"
android:text="User"
android:id="@+id/pUser"
android:textColor="@color/white"
android:textSize="17dp"
android:background="@drawable/sidebar_puser"
android:gravity="center_vertical|left"
android:layout_below="@+id/pLibButton"
android:paddingLeft="80dp" />
<com.breadcreative.Presenter.Core.SlidingMenuButton
android:layout_width="wrap_content"
android:layout_height="60dp"
android:text="Shared"
android:id="@+id/pShared"
android:textColor="@color/white"
android:textSize="17dp"
android:background="@drawable/sidebar_pshared"
android:paddingLeft="80dp"
android:gravity="center_vertical|left"
android:layout_below="@+id/pUser" />
<TextView
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="DOCUMENTS"
android:id="@+id/textView"
android:textColor="@color/white"
android:gravity="center_vertical"
android:textSize="15sp"
android:paddingLeft="32dp"
android:layout_below="@+id/pShared"
android:layout_marginTop="5dp" />
<com.breadcreative.Presenter.Core.SlidingMenuButton
android:layout_width="wrap_content"
android:layout_height="60dp"
android:text="Library"
android:id="@+id/dLibraryBtn"
android:textSize="17dp"
android:textColor="@color/white"
android:background="@drawable/sidebar_dlib"
android:gravity="center_vertical|left"
android:paddingLeft="80dp"
android:layout_below="@+id/textView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="MESSAGES"
android:id="@+id/textView2"
android:paddingLeft="32dp"
android:gravity="center_vertical"
android:textSize="15sp"
android:textColor="@color/white"
android:layout_below="@+id/dLibraryBtn"
android:layout_marginTop="5dp" />
<com.breadcreative.Presenter.Core.SlidingMenuButton
android:layout_width="wrap_content"
android:layout_height="60dp"
android:text="Inbox"
android:id="@+id/messages_btn"
android:textSize="17dp"
android:textColor="@color/white"
android:gravity="center_vertical|left"
android:background="@drawable/sidebar_messages"
android:paddingLeft="80dp"
android:layout_below="@+id/textView2" />
我的问题是,如果我设置了
android:layout_width="wrap_content"
自定义按钮太小,但如果我将其设置为
android:layout_width="match_parent"
按钮宽度与设备屏幕相同,而不是我想要的自定义RelativeLayout。我错过了什么?