我有一个应用程序,有一个3图像按钮和横幅广告下面的活动。就像这样..。
ImageButton布局大小是包装内容,每个源文件都是我制作的一个png。现在,当我在一个小屏幕上测试时,底部的屏幕在横幅广告的后面。我想根据屏幕的大小(主要是高度)来缩放ImageButtons。
有人能告诉我做这件事的正确方法吗?
我必须制作适合小屏幕的小png源,然后制作更多的带有最小屏幕宽度限定符的绘图文件- 320dpi和480dpi这真的是一个很大的边际,我将不得不在两者之间制作更多的文件夹。
提前感谢大家!
发布于 2020-01-30 01:01:03
如果你没有域约束布局,也许你可以尝试在你的主布局中使用一个带权重的LinearLayout来划分屏幕……如下所示,它可能会起作用:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="4"
android:padding="10dp">
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@drawable/..."/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@drawable/..."/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@drawable/..."/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@drawable/..."/>
</LinearLayout>
</RelativeLayout>
只要你想调整它就行了.希望这能起作用:)
发布于 2020-01-30 01:05:17
你不需要创建单独的图像,你可以使用ConstraintLayout
,它会自动为你管理大小。我已经测试了下面的代码,它似乎工作得很好。试一试:
your_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/imageButton2"
android:background="@color/white"
android:src="@drawable/image_1"/>
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/imageButton1"
app:layout_constraintBottom_toTopOf="@id/imageButton3"
android:background="@color/white"
android:src="@drawable/image_2"/>
<ImageButton
android:id="@+id/imageButton3"
android:layout_width="wrap_content"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/imageButton2"
app:layout_constraintBottom_toTopOf="@id/bannerContainer"
android:background="@color/white"
android:src="@drawable/image_3"/>
<!-- Whatever container your banner is in -->
<RelativeLayout
android:id="@+id/bannerContainer"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#000"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
注意:我添加了背景颜色只是为了测试一下。您要关注的要点是,ImageButton
的高度对所有人都是0dp
,并且约束自动管理高度。
ConstraintLayout
的好处是,您不需要像在LinearLayout
中那样指定不同的权重,因此您可以拥有不同高度的图像,它将工作得很好。
发布于 2020-01-30 00:23:14
如果使用的是css,则可以使用特性单位vh (视口高度)。该测量值是视口/屏幕高度的百分比。例如,height: 10vh; width: auto;
会将元素高度渲染为屏幕高度的10%,而不会扭曲您的png。如果您对所示的四个元素使用此测量,则当它们的值加起来为100时,它们都将同时出现在屏幕上。
https://stackoverflow.com/questions/59977390
复制相似问题