Java 相对线和画布肋视图
我想用这种结构做一个活动: 想法是在顶部放置一些文本,在顶部始终放置一个横幅,横幅上的按钮和视图中心的“我的画布”,使用所有可能的空间 我正在子类化视图以绘制画布,并重载onMeasure()方法: 这是我的XML:Java 相对线和画布肋视图,java,android,android-relativelayout,Java,Android,Android Relativelayout,我想用这种结构做一个活动: 想法是在顶部放置一些文本,在顶部始终放置一个横幅,横幅上的按钮和视图中心的“我的画布”,使用所有可能的空间 我正在子类化视图以绘制画布,并重载onMeasure()方法: 这是我的XML: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:a
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".DrawLevelActivity"
android:background="@color/activity_background">
<TextView
android:id="@+id/instructionsText"
android:gravity="center"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<com.mycanvas.BoardCanvas
android:id="@+id/boardCanvas"
android:gravity="center"
android:layout_below="@+id/instructionsText"
android:layout_centerVertical="true"
android:padding="0dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/solveButton"
android:gravity="center"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:layout_below="@+id/boardCanvas"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<com.google.ads.AdView
xmlns:googleads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="@+id/adView"
android:gravity="center"
android:paddingTop="0dp"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:layout_alignParentBottom="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
googleads:adSize="BANNER"
googleads:adUnitId="@string/admob_id" />
</RelativeLayout>
不幸的是,按钮出现在横幅下,我认为原因是画布非常大
onMeasure()中的设置值始终小于MeasureSpec.getSize(heightMeasureSpec)这里的关键是使用
线性布局
仅为画布设置布局重量
值。这样,顶部和上方视图将包装其内容,画布将填充可用空间。布局xml如下所示-
<LinearLayout 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"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical|center_horizontal"
android:background="#990099"
android:text="Some text here" />
<RelativeLayout
android:id="@+id/canvas"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#999900"
android:layout_weight="1">
<!-- The canvas -->
</RelativeLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
<RelativeLayout
android:id="@+id/banner"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#009999">
<!-- The banner -->
</RelativeLayout>
</LinearLayout>
请注意,我设置了一些硬编码的高度和颜色,以便您可以轻松查看结果。此布局将呈现如下视图-
<LinearLayout 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"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical|center_horizontal"
android:background="#990099"
android:text="Some text here" />
<RelativeLayout
android:id="@+id/canvas"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#999900"
android:layout_weight="1">
<!-- The canvas -->
</RelativeLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
<RelativeLayout
android:id="@+id/banner"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#009999">
<!-- The banner -->
</RelativeLayout>
</LinearLayout>
非常感谢@ssantos,它就像一个符咒!太好了,这很有帮助。享受吧!