安卓约束布局高度百分比 0.99



当 app:layout_constraintHeight_percent 为 0.50 或 0.80 时,它可以正常工作,但是当我将其设置为 0.99 时,按钮很长并且大于屏幕的 99% ?

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fillViewport="true"
android:orientation="vertical">

<android.support.constraint.ConstraintLayout
android:orientation="vertical"
android:background="@color/sos"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<Button
android:id="@+id/mybtn1"
android:text="1"
android:layout_width="match_parent"
app:layout_constraintHeight_percent="0.99"
android:layout_height="0dp" />
<Button
app:layout_constraintTop_toBottomOf="@+id/mybtn1"
android:text="2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</android.support.constraint.ConstraintLayout>

</ScrollView>

这应该有效;

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button 1" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button 2" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>

试试这段代码:

<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<android.support.constraint.ConstraintLayout
android:background="@android:color/holo_blue_dark"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_one"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintHeight_percent="0.91"
android:text="1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/btn_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn_one"
app:layout_constraintVertical_bias="0.0"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
</android.support.constraint.ConstraintLayout>
</ScrollView>

我希望它对你有所帮助。

问题不在于99%的layout_constraintHeight_percent,而在于当两个按钮的总高度超过ScrollView屏幕上的高度时。碰巧 99% 会在您的模拟器/设备上触发这种任性行为。如果使第二个按钮更高,您将看到相同的行为,但百分比较小。

我发现这些类型的布局是有问题的,因为孩子的测量取决于父的大小,而父的大小取决于孩子的大小。在布局中,子mybtn1取决于父ConstraintLayout(app:layout_constraintHeight_percent="0.99"(,而父ConstraintLayout取决于子mybtn1的大小 (android:layout_height="wrap_content"(。当两个按钮的总高度不超过ScrollView的屏幕高度时,这似乎工作正常,但当组合高度超过它时,这似乎可以正常工作。(当组合高度小于ScrollView高度时,在ScrollView上设置android:fillViewport="true"可能会起作用,因为ScrollView有一个隐式的固定高度,这只是屏幕上的可用空间。不过,这只是猜想。

您可以通过一些编码来解决此问题,如下所示:

将 id 添加到ScrollView

android:id="@+id/scrollView"

将以下代码放在onCreate()

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ScrollView scrollView = findViewById(R.id.scrollView);
scrollView.post(new Runnable() {
@Override
public void run() {
Button button = findViewById(R.id.mybtn1);
ConstraintLayout.LayoutParams lp = (ConstraintLayout.LayoutParams) button.getLayoutParams();
// Get percentage height of button.
float percent = lp.matchConstraintPercentHeight;
// Explicitly set the button height based upon the ScrollView height.
button.setHeight((int) (scrollView.getHeight() * percent));
// Reset the percent height so it no longer has any effect.
lp.matchConstraintPercentHeight = 1.0f;
}
});
}

此代码将强制mybtn1的大小为ScrollView高度的 99% 或为按钮指定的任何百分比。

最新更新