Android布局异常-实际布局不符合Android Studio外观。
我正在尝试编写一个应用程序,其中包含两个独立的图形数据视图——一个是X-Y图。另一个是X对T图。我希望这两个垂直排列,上面有X-Y图。我已经为管理绘图的View类编写了一个扩展,这不是问题所在 我最初使用的是RelativeLayout:Android布局异常-实际布局不符合Android Studio外观。,android,android-layout,Android,Android Layout,我正在尝试编写一个应用程序,其中包含两个独立的图形数据视图——一个是X-Y图。另一个是X对T图。我希望这两个垂直排列,上面有X-Y图。我已经为管理绘图的View类编写了一个扩展,这不是问题所在 我最初使用的是RelativeLayout: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#CCCCFF"
android:orientation="vertical"
android:gravity="center_horizontal">
<RelativeLayout
android:id="@+id/button_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clearCanvas"
android:text="@string/btn1Text"
style="@android:style/Widget.Button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="startPlot"
android:text="@string/btn2Text"
android:layout_toRightOf="@id/button1"
style="@android:style/Widget.Button" />
</RelativeLayout>
<co.android.bmd.chaoswithtime.CanvasView
android:id="@+id/plot_canvas"
android:layout_width="1000px"
android:layout_height="1000px"
android:textColor="#FFFFFF" />
<co.android.bmd.chaoswithtime.CanvasView
android:id="@+id/trend_canvas"
android:textColor="#FFFFFF"
tools:layout_below="@id/plot_canvas"
android:layout_height="500px"
android:layout_width="1000px" />
</RelativeLayout
您已经以像素为单位传递了宽度和高度,这意味着它是内容,当您运行代码时,屏幕上只需要1000像素
更好的方法是使用LinearLayout
作为自定义视图的父视图,并使用weight和weightSum
。并将match_parent
宽度和高度传递给LinearLayout
您可以尝试使用下面的xml。这可以帮助你
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:custom="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#CCCCFF"
android:gravity="center_horizontal"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/button_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<Button
android:id="@+id/button1"
style="@android:style/Widget.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clearCanvas"
android:text="@string/btn1Text" />
<Button
android:id="@+id/button2"
style="@android:style/Widget.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/button1"
android:onClick="startPlot"
android:text="@string/btn2Text" />
</RelativeLayout>
<LinearLayout
android:layout_below="@+id/button_bar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="3">
<co.android.bmd.chaoswithtime.CanvasView
android:id="@+id/plot_canvas"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:textColor="#FFFFFF" />
<co.android.bmd.chaoswithtime.CanvasView
android:id="@+id/trend_canvas"
android:layout_height="0dp"
android:layout_weight="2"
android:textColor="#FFFFFF"
tools:layout_below="@id/plot_canvas"1
android:layout_width="match_parent" />
</LinearLayout>
</RelativeLayout
您已经以像素为单位传递了宽度和高度,这意味着它是内容,当您运行代码时,屏幕上只需要1000像素
更好的方法是使用LinearLayout
作为自定义视图的父视图,并使用weight和weightSum
。并将match_parent
宽度和高度传递给LinearLayout
您可以尝试使用下面的xml。这可以帮助你
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:custom="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#CCCCFF"
android:gravity="center_horizontal"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/button_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<Button
android:id="@+id/button1"
style="@android:style/Widget.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clearCanvas"
android:text="@string/btn1Text" />
<Button
android:id="@+id/button2"
style="@android:style/Widget.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/button1"
android:onClick="startPlot"
android:text="@string/btn2Text" />
</RelativeLayout>
<LinearLayout
android:layout_below="@+id/button_bar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="3">
<co.android.bmd.chaoswithtime.CanvasView
android:id="@+id/plot_canvas"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:textColor="#FFFFFF" />
<co.android.bmd.chaoswithtime.CanvasView
android:id="@+id/trend_canvas"
android:layout_height="0dp"
android:layout_weight="2"
android:textColor="#FFFFFF"
tools:layout_below="@id/plot_canvas"1
android:layout_width="match_parent" />
</LinearLayout>
</RelativeLayout
为什么工具:layout_在下面而不是android:layout_在下面?这是我从AS中的布局包中得到的-它已经修复了绘图顺序。但是现在这两个图形面板是左对齐的,而不是中间对齐的我可以设法得到我想要的布局-但我最初的问题仍然存在-为什么Android Studio preview与设备上的布局不匹配?为什么工具:layout_在下面而不是Android:layout_在下面?这是我在AS中从布局包中得到的-它修复了绘图顺序。但是现在这两个图形面板是左对齐的,而不是中间对齐的我可以设法得到我想要的布局-但我最初的问题仍然存在-为什么Android Studio preview与设备上的布局不匹配?在这个阶段,我使用一个固定的布局和一个目标-一旦我得到基本问题的排序。我将研究如何添加代码来处理不同的目标。@user1815293请检查上面的xml并尝试使用它。在这个阶段,我将使用一个固定的布局和一个目标-一旦我对基本问题进行了排序。我将研究如何添加代码来处理不同的目标。@user1815293请检查上面的xml并尝试使用它。