【发布时间】:2018-05-04 15:32:38
【问题描述】:
我正在尝试实现一个与此类似的屏幕:
我能够实现上述。我使用了以下逻辑:
FrameLayout
|- Linear Layout
|- Button (Purple Colour, Layout Width = 1)
|- Button (Blue Colour, Layout Width = 1)
|- Linear Layout (Layout_Gravity = center)
|- Button (Button Text)
源代码
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/accent_material_light"
android:orientation="vertical"
android:padding="0dp"
android:weightSum="100">
<ImageButton
android:id="@+id/imageButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="40"
android:background="@android:color/holo_purple" />
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="60"
android:background="@android:color/holo_blue_light" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:background="@color/abc_search_url_text_pressed">
<Button
android:id="@+id/button2"
android:layout_height="48dp"
android:text="Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_width="match_parent" />
</LinearLayout>
</FrameLayout>
问题:-
如果两个视图(紫色和蓝色视图)必须分别占据屏幕的 40% 和 60%,我应该如何将 Button 放置在两个视图的边框上。在这种情况下,我不能使用 layout_gravity 居中。如何解决这个问题?
看起来像这样:-
如何解决这个问题?
【问题讨论】:
-
对我来说,我会将
FrameLayout替换为RelativeLayout,删除第二个LinearLayout并在按钮上添加`android:layout_below="@+id/imageButton"` 并且可能会给一些上边距。
标签: android layout android-linearlayout android-framelayout