修复 Android 布局(dp、边距等)
Fix Android layout (dp, margin etc.)
尽管我自己阅读了几个线程来解释 dp、sp、px 等之间的差异,并且还试图获得一个好的布局,
我仍在努力为不同的手机完成它。
我正在使用不同的片段,但如果我得到帮助以在一个片段中完成所有工作,我相信我将能够自己修复其他片段。
<RelativeLayout 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"
<!--all paddings=0-->
android:keepScreenOn="true"
tools:context=".MainActivity">
...
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragmentBase"
class="...FragmentBase" />
<!--other fragments-->
</RelativeLayout>
所以我正在尝试在该 fragmentBase 中获得正确的布局,如下所示:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
<!--all paddings=0-->>
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragmentBase"
class="...FragmentBase" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_marginTop="40dp"
android:requiresFadingEdge="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!--elements-->
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom">
<Button
android:layout_width="0dp"
android:layout_weight="0.8"
android:layout_height="40dp"
<!--other stuff-->/>
<Button
android:layout_width="0dp"
android:layout_weight="0.5"
android:layout_height="40dp"
<!--other stuff--> />
<Button
android:layout_width="0dp"
android:layout_weight="0.7"
android:layout_height="40dp"
<!--other stuff-->/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:layout_marginBottom="40dp">
<Button
android:layout_width="0dp"
android:layout_weight="0.6"
android:layout_height="40dp"
<!--other stuff--> />
<Button
android:layout_width="0dp"
android:layout_weight="0.5"
android:layout_height="40dp"
<!--other stuff--> />
<Button
android:layout_width="0dp"
android:layout_weight="0.6"
android:layout_height="40dp"
<!--other stuff--> />
</LinearLayout>
</RelativeLayout>
太棒了。问题基本上是,滚动视图在某些情况下太长,在其他情况下太短。固定 layout_height="400dp" 似乎是一个错误。
还将两个按钮行的高度设置为 40dp 并且一个在另一个之上 40dp,从底部开始似乎不是一个好的决定,但是那些按钮有背景图像,所以我不能使用wrap_content.
如何布局才正确?我希望 scrollView 在我的 "header" 正下方开始,因此 android:layout_marginTop="40dp" 或多或少是固定的,而两个按钮行应该位于屏幕的最底部。
我想我希望滚动视图是动态长度的。
请帮忙:S
尝试将此代码添加到您的滚动视图中
android:layout_toStartOf="@+id/linearlayout1"
还有这个
android:id="@+id/linearlayout1"
到您的 40 页边距布局
这可能有帮助
我会尽力帮助您,而无需重写上面的所有代码。
首先,我将首先提出一些更改建议:
您可以将包含按钮的两个 LinearLayouts 的高度设置为 40dp,并将按钮的高度设置为 "match_parent"
你可以给两个包含按钮的 LinearLayouts 一个 id,比方说
android:id="@+id/row1"
和
android:id="@+id/row2"
然后设置最下面的(id=row2)有
android:layout_alignParentBottom="true"
而最上面的 (id=row1) 有
android:layout_above="@id/row2"
完成后,您可以在 ScrollView 中设置以下参数
android:layout_alignParentTop="true"
android:layout_above="@id/row1"
如果我理解了这个问题,这应该会有所帮助。希望我写的有用
尽管我自己阅读了几个线程来解释 dp、sp、px 等之间的差异,并且还试图获得一个好的布局, 我仍在努力为不同的手机完成它。
我正在使用不同的片段,但如果我得到帮助以在一个片段中完成所有工作,我相信我将能够自己修复其他片段。
<RelativeLayout 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"
<!--all paddings=0-->
android:keepScreenOn="true"
tools:context=".MainActivity">
...
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragmentBase"
class="...FragmentBase" />
<!--other fragments-->
</RelativeLayout>
所以我正在尝试在该 fragmentBase 中获得正确的布局,如下所示:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
<!--all paddings=0-->>
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragmentBase"
class="...FragmentBase" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_marginTop="40dp"
android:requiresFadingEdge="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!--elements-->
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom">
<Button
android:layout_width="0dp"
android:layout_weight="0.8"
android:layout_height="40dp"
<!--other stuff-->/>
<Button
android:layout_width="0dp"
android:layout_weight="0.5"
android:layout_height="40dp"
<!--other stuff--> />
<Button
android:layout_width="0dp"
android:layout_weight="0.7"
android:layout_height="40dp"
<!--other stuff-->/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:layout_marginBottom="40dp">
<Button
android:layout_width="0dp"
android:layout_weight="0.6"
android:layout_height="40dp"
<!--other stuff--> />
<Button
android:layout_width="0dp"
android:layout_weight="0.5"
android:layout_height="40dp"
<!--other stuff--> />
<Button
android:layout_width="0dp"
android:layout_weight="0.6"
android:layout_height="40dp"
<!--other stuff--> />
</LinearLayout>
</RelativeLayout>
太棒了。问题基本上是,滚动视图在某些情况下太长,在其他情况下太短。固定 layout_height="400dp" 似乎是一个错误。 还将两个按钮行的高度设置为 40dp 并且一个在另一个之上 40dp,从底部开始似乎不是一个好的决定,但是那些按钮有背景图像,所以我不能使用wrap_content.
如何布局才正确?我希望 scrollView 在我的 "header" 正下方开始,因此 android:layout_marginTop="40dp" 或多或少是固定的,而两个按钮行应该位于屏幕的最底部。 我想我希望滚动视图是动态长度的。
请帮忙:S
尝试将此代码添加到您的滚动视图中
android:layout_toStartOf="@+id/linearlayout1"
还有这个
android:id="@+id/linearlayout1"
到您的 40 页边距布局
这可能有帮助
我会尽力帮助您,而无需重写上面的所有代码。
首先,我将首先提出一些更改建议:
您可以将包含按钮的两个 LinearLayouts 的高度设置为 40dp,并将按钮的高度设置为 "match_parent"
你可以给两个包含按钮的 LinearLayouts 一个 id,比方说
android:id="@+id/row1"
和
android:id="@+id/row2"
然后设置最下面的(id=row2)有
android:layout_alignParentBottom="true"
而最上面的 (id=row1) 有
android:layout_above="@id/row2"
完成后,您可以在 ScrollView 中设置以下参数
android:layout_alignParentTop="true" android:layout_above="@id/row1"
如果我理解了这个问题,这应该会有所帮助。希望我写的有用