android - 按百分比划分屏幕

标签 android android-layout

我正在尝试在垂直布局 3 个 View 时创建一个相对布局。第一个 View 是文本,应该占屏幕的 10%。第二个 View 是图像,应该占据屏幕的 50%。第三个 View 是文本,应该占据剩余的 40%。

我的做法是通过手动调整图片的尺寸,通过观察估计它接近我想要的。我确信这不是正确的方法。那我该怎么做呢?基本上我怎样才能明智地划分屏幕百分比,我可以用相对布局来做到这一点

谢谢

最佳答案

您应该将 LinearLayoutandroid:weightSum 一起使用。

在根布局上使用 android:weightSum="100" 并根据您的要求提供 android:layout_weight。和 android:layout_height="0dp" 用于每个 View

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="100" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="10" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="50"
        android:contentDescription="@string/image"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="40" />

</LinearLayout>

希望对您有所帮助。

关于android - 按百分比划分屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20029179/

相关文章:

android - Android Activity 上的字母被 block 替换

android - 如何在自定义对话框中设置监听器...?

不会在 Activity 之间重新加载的 Android 导航栏

安卓 Wi-Fi 直连 : WifiP2pInfo object passed on to onConnectionInfoAvailable callback has null groupOwnerAddress property

android - 将 CameraX + CameraView 绑定(bind)到生命周期时出现问题

android - 无法解析方法setReverseLayout和setStackFromEnd?

java - Android 应用程序 - 后退按钮

java.lang.ClassCastException : android. widget.TextView

java - 我的应用程序在 android studio 中崩溃了,在查看日志时我无法弄清楚出了什么问题

android - RxJava- 动态 EditText 上的 RxAndroid 表单验证