使用RelativElayout或ConstraintLayout在ScrollView内显示视图未正确显示
我正在尝试实现这一目标...
好的,这看起来不错,没问题..但是
这是问题,如果这是在小屏幕上
我试图用 relativelayout
和约束layout
实现这一目标,但效果是相同的。
XML源代码:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
android:background="#959494"
android:fillViewport="true"
android:scrollbars="none">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/_12sdp">
<com.google.android.material.card.MaterialCardView
android:id="@+id/box1"
android:layout_width="160dp"
android:layout_height="160dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Box 1"
android:textColor="#000"
android:textSize="30sp" />
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:id="@+id/box2"
android:layout_width="160dp"
android:layout_height="160dp"
android:layout_below="@id/box1"
android:layout_marginTop="12dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Box 2"
android:textColor="#000"
android:textSize="30sp" />
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:id="@+id/box3"
android:layout_width="160dp"
android:layout_height="160dp"
android:layout_below="@id/box2"
android:layout_marginTop="12dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Box 3"
android:textColor="#000"
android:textSize="30sp" />
</com.google.android.material.card.MaterialCardView>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/box3"
android:gravity="bottom"
android:layout_alignParentBottom="true"
android:layout_marginTop="@dimen/_12sdp"
android:textColor="#fff"
android:textSize="30sp"
tools:text="This is a Textview" />
</RelativeLayout>
</ScrollView>
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
android:background="#959494"
android:fillViewport="true"
android:scrollbars="none">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/_12sdp">
<com.google.android.material.card.MaterialCardView
android:id="@+id/box1"
android:layout_width="160dp"
android:layout_height="160dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Box 1"
android:textColor="#000"
android:textSize="30sp" />
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:id="@+id/box2"
android:layout_width="160dp"
android:layout_height="160dp"
android:layout_marginTop="12dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/box1">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Box 2"
android:textColor="#000"
android:textSize="30sp" />
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:id="@+id/box3"
android:layout_width="160dp"
android:layout_height="160dp"
android:layout_marginTop="12dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/box2">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Box 3"
android:textColor="#000"
android:textSize="30sp" />
</com.google.android.material.card.MaterialCardView>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom"
android:textColor="#fff"
android:layout_marginTop="@dimen/_12sdp"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/box3"
tools:text="This is a Textview" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在这里尝试一下,它应该有效,并让我知道它是否有效。
Here Try This, It Should Work and let me know if it works or not.
这不是答案,而是要清除一些要点,为了清洁,我必须将其发布为答案...
如果您查看上述代码,这对小屏幕来说很好,但是在大屏幕上可以,最后一个视图只是将其位置调整到框3视图的中心和屏幕末端,但我想要的是屏幕的结束。

我们可以通过将
textView1
高度更改为0dp来实现这一目标,但是结果就像仅在较大屏幕上工作并隐藏在小屏幕上的最后一个视图相反。当设置
textView1
高度为0dp时。结果与我说的相反。This is not an answer BUT to clear some points and for cleanness, I have to post it as an answer...
If you look at the above code this is working fine for small screens BUT on large screens, the last view is just adjusted its position to the center of the Box 3 view and end of the screen BUT what I want is to end of the screen.

We can achieve this by changing
textView1
height to 0dp but then the results went opposite like working on a bigger screen only and hiding the last view on the small screen.And When setting

textView1
height to 0dp.. results are the opposite as I said.该解决方案使用
约束布局垂直偏置
This solution worked for me using
constraint layout vertical bias