我为Action Bar创建了一个自定义布局:我在Activity上添加了这段代码:
LayoutInflater inflater = (LayoutInflater)getSystemService (Context.LAYOUT_INFLATER_SERVICE);
View actionBarView = inflater.inflate (R.layout.main_actionbar, null);
getSupportActionBar ().setCustomView (actionBarView);
和main_actionbar.xml布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:style="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white" >
<RelativeLayout
android:id="@+id/leftIcons"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toLeftOf="@+id/empty"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:background="@drawable/uiclickable_gray_states"
android:layout_alignParentLeft="true" >
<ImageView
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:layout_marginRight="5dp"
android:src="@drawable/icon_back" />
<!-- The problem is here -->
<ImageView
android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/back"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:src="@drawable/bg"
android:scaleType="fitXY"
android:adjustViewBounds="true" />
</RelativeLayout>
<View
android:id="@+id/empty"
android:layout_width="100dp"
android:layout_height="match_parent"
android:background="@color/black_transparent"
android:layout_alignParentRight="true" />
</RelativeLayout>
我的问题是,我希望@+id/thumbnail
将其高度设为match_parent
,并让它的宽度等于高度。意思是:height == match_parent AND width == height
,所以我可以有一个方形图像。
似乎这不能通过xml完成,所以我创建了(在网络上的一些教程的帮助下)ImageView的子类并做了这个但没有任何积极的结果:
@Override
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {
//int width = measureWidth (widthMeasureSpec);
int height = measureHeight (heightMeasureSpec);
setMeasuredDimension (height, height);
}
private int measureHeight (int measureSpecHeight) {
int result;
int specMode = MeasureSpec.getMode (measureSpecHeight);
int specSize = MeasureSpec.getSize (measureSpecHeight);
if (specMode == MeasureSpec.EXACTLY) {
// We were told how big to be
result = specSize;
} else if (specMode == MeasureSpec.AT_MOST) {
// The child can be as large as it wants up to the specified size.
result = specSize;
} else {
// Measure the text (beware: ascent is a negative number)
result = canvasSize;
}
return (result + 2);
}
我在那里做错了吗?谢谢。