Java 如何根据屏幕大小动态改变高度?
我有一个相对论,有一个ViewPager和另一个LinearLayout。我已将ViewPager的布局宽度设置为match_parent,并将布局高度设置为固定值250dp 但是,当屏幕尺寸越来越大时,ViewPager的高度会越来越小 如何根据屏幕大小改变高度 如果我将高度设置为wrap_内容,它几乎占据了整个屏幕,除了为其他布局保留的较小部分 代码发布在下面Java 如何根据屏幕大小动态改变高度?,java,android,xml,Java,Android,Xml,我有一个相对论,有一个ViewPager和另一个LinearLayout。我已将ViewPager的布局宽度设置为match_parent,并将布局高度设置为固定值250dp 但是,当屏幕尺寸越来越大时,ViewPager的高度会越来越小 如何根据屏幕大小改变高度 如果我将高度设置为wrap_内容,它几乎占据了整个屏幕,除了为其他布局保留的较小部分 代码发布在下面 <RelativeLayout xmlns:android="http://schemas.android.com/apk/r
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.sloid.destinations.navigationdrawerfragments.aboutSLFragment">
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_marginBottom="8dp"/>
<LinearLayout
android:id="@+id/SliderDots"
android:layout_below="@+id/viewPager"
android:orientation="horizontal"
android:gravity="center_vertical|center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
试试这个
如果您想为多台设备进行屏幕设计
您可以使用WeightSum和LinearLayout作为容器来避免这种情况,如下所示:-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="10"
tools:context="com.example.sloid.destinations.navigationdrawerfragments.aboutSLFragment">
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3.5"
android:layout_marginBottom="8dp"/>
<LinearLayout
android:id="@+id/SliderDots"
android:layout_below="@+id/viewPager"
android:orientation="horizontal"
android:gravity="center_vertical|center_horizontal"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6.5"
/>
</LinearLayout>
仅供参考:-您也可以使用现代的新ConstraintLayout 在下面的示例中,设置viewpager视图相对于屏幕高度的百分比33%
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="@+id/SliderDots"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_weight="1" />
<LinearLayout
android:id="@+id/SliderDots"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_below="@+id/viewPager"
android:gravity="center_vertical|center_horizontal"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/viewPager"
app:layout_constraintVertical_weight="2" />
</android.support.constraint.ConstraintLayout>
要严格回答您的问题: 如何根据屏幕大小改变高度 您可以将高度值外部化为default values文件夹中的dimens.xml,如
<dimen name="view_pager_height">250dp</dimen>
在values-sw720dp文件夹中,您可以将
<dimen name="view_pager_height">300dp</dimen>
然后在布局中使用它,如下所示:
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="@dimen/view_pager_height"
android:layout_marginBottom="8dp"/>
请在此处阅读更多相关信息:
除此之外,您还可以将a用作父对象,并为每个子对象设置权重。作为父母的相对年龄是相当昂贵的,因为他们会加倍测量他们的孩子将viewpager设置在幻灯片上方
为什么设置为android:layout\u height=250dp。。。有什么原因吗?你可以使用dimens属性。。另外..为不同的屏幕大小设置不同的尺寸。。如果你必须设置一个固定的屏幕大小。Thunder我不能使用layoutWeight,因为我需要添加一个scrollView。还有其他方法吗。如果我使用layoutWeights并添加一个scrollView作为父视图,它可以工作,除了一个小问题。你能解决这个问题吗。链接是Hello Deni这个答案使点指示器下降,这不是我所期望的。无论如何,使用layoutWeight修复了这个问题,但是当整个代码添加到滚动视图中时出现了一个问题。你能看一下并给出你的意见吗。链接是
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.sloid.destinations.navigationdrawerfragments.aboutSLFragment">
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/SliderDots"/>
<LinearLayout android:id="@+id/SliderDots"
android:orientation="horizontal"
android:gravity="center_vertical|center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>