【发布时间】:2018-04-02 23:27:13
【问题描述】:
在我的 android 应用程序中,我有一个包含一些片段的 RelativeLayout。但是,此布局不会扩展到不同的屏幕尺寸。当屏幕尺寸为 5.2 英寸对角线,1080x1920 分辨率 420dpi 时考虑这个屏幕截图:(理想的输出)
当我将手机更改为 5.0"、1080x1920 分辨率 xxhdpi 时,我得到了这个显示:
如您所见,右侧两列的按钮重叠,这就是我要问的问题。
这里是主要的活动 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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp">
<!--This is the box appearing at the top of the layout-->
<TextView
android:id="@+id/window"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="@drawable/window_border"
android:fontFamily="monospace"
android:minLines="1"
android:text=""
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textIsSelectable="true"/>
<!--This is the first row of buttons, there are 4 of them-->
<fragment
android:id="@+id/screenOps"
android:name="net.magnistudio.deskcalculator.ScreenOps"
android:layout_width="wrap_content"
android:layout_alignEnd="@+id/digits"
android:layout_height="wrap_content"
android:layout_below="@+id/window"
tools:layout="@layout/layout_screen_ops"/>
<!--This is the 3x3 rectangle appearing directly below the screenOps
frag -->
<fragment
android:id="@+id/digits"
android:name="net.magnistudio.deskcalculator.Digits"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:layout="@layout/layout_digits"
android:layout_alignBottom="@+id/basicOps"/>
<!--This is the rightmost fragment-->
<fragment
android:id="@+id/basicOps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="net.magnistudio.deskcalculator.BasicOps"
tools:layout="@layout/layout_basic_ops"
android:layout_alignParentEnd="true"
android:layout_below="@+id/window"/>
<!--Lower fragment, it is 6x4-->
<fragment
android:id="@+id/scientific"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="net.magnistudio.deskcalculator.Scientific"
tools:layout="@layout/layout_scientific"
android:layout_below="@+id/digits"/>
</RelativeLayout>
每个按钮都有这种风格
<style name="calcBtnAppearance">
<item name="android:textSize">14sp</item>
<item name="android:textColor">@color/calcBtn</item>
<item name="android:textStyle">normal</item>
<item name="android:textAllCaps">false</item>
<item name="android:layout_width">0dp</item>
</style>
我认为一种解决方案是根据当前手机的大小以编程方式调整布局的大小,但也许这只是解决了一些其他问题(布局本身)?
此外,每个按钮都位于一个 LinearLayout 内,该 LinearLayout 位于片段的 LinearLayout 内。考虑以下片段布局文件的示例摘录:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="3">
<Button
android:id="@+id/dig7"
style="@style/calcBtnAppearance"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""/>
more buttons
</LinearLayout>
<LinearLayout>
【问题讨论】: