Android 使用百分比设置填充
我想在android中设置一个元素的填充,以便在左、右和底部填充3.2%,我做了一些研究,但没有得到任何有趣的结果,这里是我尝试过的Android 使用百分比设置填充,android,Android,我想在android中设置一个元素的填充,以便在左、右和底部填充3.2%,我做了一些研究,但没有得到任何有趣的结果,这里是我尝试过的 Display display = getWindowManager().getDefaultDisplay(); int width = display.getWidth(); int height = display.getHeight();
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
FrameLayout rel = (FrameLayout) findViewById(R.id.tabcontent);
double width_pas= width*3.2;
double height_pas=height*3.2;
rel.setPadding(width_pas,0,width_pas,height_pas);
但是我在setpadding中得到错误,它只接受整数,但我需要将边距设置为3.2
希望你能帮助我。三件事:
0.032
,而不是乘以3.2
ints
而不是float/double
的话,您最终会得到3%
的填充而不是3.2%
,这是不正确的。这取决于你要做什么。如果您这样计算填充物:您将得到尽可能精确的3.2%的高度/宽度。您可以通过
只能通过设置填充功能设置像素计数。所以像素数是整数。你不能给双像素。你得想办法把它投出去。为什么你不做“int width_pas=(int)(width*3.2);”如果我这样做,我会得到3%的填充,而不是问题的3.2%,所以我只需要知道是否有解决这个问题的替代方案。没有替代方案。像素是屏幕上可以设置颜色的最小部分。所以,不能在半像素上设置某些内容。您必须精确指定要绘制的像素。顺便说一句,铸造并不意味着%3。
int width_pas = Math.round(width * 0.032f);
int height_pas = Math.round(height * 0.032f);
<?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.constraint.Guideline
android:id="@+id/startVerticalGuideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.032" />
<android.support.constraint.Guideline
android:id="@+id/endVerticalGuideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.968" />
<android.support.constraint.Guideline
android:id="@+id/bottomGuideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.968" />
<FrameLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#AAA"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toEndOf="@+id/startVerticalGuideline"
app:layout_constraintEnd_toStartOf="@+id/endVerticalGuideline"
app:layout_constraintBottom_toTopOf="@+id/bottomGuideline" />
</android.support.constraint.ConstraintLayout>