为了账号安全,请及时绑定邮箱和手机立即绑定

Android 不会根据不同的屏幕布局调整大小

Android 不会根据不同的屏幕布局调整大小

江户川乱折腾 2023-05-17 14:52:03
我正在尝试根据我使用的设备调整布局大小。我正在测试两种不同的手机(三星 Galaxy S8+ 和索尼 Xperia Z2(甚至是两种不同的模拟器))。我在清单文件中添加了一些代码。我创建了具有正确(假定)名称的不同文件夹(layout、layout-small、layout-large、layout-xlarge)。我将我的代码放入值文件夹,文件“dimens.xml”并从我的布局中调用它。但似乎没有任何效果。我的屏幕唯一一次发生变化是在我将相应的文件修改到“布局”文件夹中时。我会喜欢任何帮助或建议。谢谢你!我已经多次阅读有关 Android Developer 和 Stack Overflow 的文档,但似乎找不到解决方案。AndroidManifest.xml 的片段<manifest xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    package="com.example.applicationpro">    <supports-screens android:smallScreens="true"        android:normalScreens="true"        android:largeScreens="true"        android:xlargeScreens="true"        android:anyDensity="true"        android:resizeable="true" />    <uses-permission android:name="android.permission.CAMERA" />    <uses-permission android:name="android.permission.GET_ACCOUNTS" />res/layout-large/my_layout.xml 的片段<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/mainLL"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@drawable/bg4"    android:orientation="vertical"    tools:context=".activity.LoginActivity">    <ProgressBar        android:id="@+id/loginPG"        android:theme="@style/FragmentsProgressBar"        style="@style/Widget.AppCompat.ProgressBar"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:visibility="gone"        android:layout_marginTop="100dp"/>        <android.support.design.widget.TextInputLayout            android:id="@+id/loginTIY"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginTop="@dimen/margin_top_large">        </android.support.design.widget.TextInputLayout>
查看完整描述

1 回答

?
侃侃无极

TA贡献2051条经验 获得超10个赞

TL;DR 这是另一种方法 ConstraintLayout,不是针对每种屏幕尺寸使用单一布局


您可以使用ConstraintLayout支持不同的屏幕尺寸:

ConstraintLayout 允许您创建具有平面视图层次结构(无嵌套视图组)的大型复杂布局。它与 RelativeLayout 相似,因为所有视图都是根据兄弟视图和父布局之间的关系进行布局,但它比 RelativeLayout 更灵活,并且更容易与 Android Studio 的布局编辑器一起使用。

您可以使用这些属性以百分比指定视图大小:

app:layout_constraintWidth_percent=".5"
app:layout_constraintHeight_percent=".5"

例如,单个按钮的高度和宽度都等于屏幕的 50%:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent">


<Button
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintWidth_percent=".5"
    app:layout_constraintHeight_percent=".5"/>

</androidx.constraintlayout.widget.ConstraintLayout>

它看起来像这样:

//img4.sycdn.imooc.com/646479c30001310303540628.jpg

我可以推荐使用ConstraintLayout with guidelines和Chains来支持不同的屏幕尺寸(除了我上面提到的 -app:layout_constraintWidth_percentapp:layout_constraintHeight_percent)。


乍一看,这可能需要大量工作,有些人可能想知道这是否真的值得付出努力,但这就是为什么我相信 ConstraintLayout 是构建 UI 的正确方法:

  • 它真的很用户友好。

  • ConstraintLayout 非常简单易学。

  • 一旦你学会了它,你会发现你节省了大量的开发时间,因为制作你的 UI 非常简单。

  • 约束布局旨在支持不同的屏幕尺寸,因此无需为每个屏幕尺寸构建布局(这也与之前的优势相关——节省开发时间)。


查看完整回答
反对 回复 2023-05-17
  • 1 回答
  • 0 关注
  • 100 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信