RelativeLayout 或 TableLayout Android 3 个按钮

RelativeLayout or TableLayout Android 3 buttons

我想做一些简单的事情,1 列 3 个按钮,反应灵敏,就像在屏幕上一样。

我应该像在我的代码中那样在 RelativeLayout 上创建它,还是 TableLayout 更好?

如何link RelativeLayout 到顶部和底部,以确保最后一个按钮永远不会超出屏幕底部?

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main"
tools:context=".MainActivity"
android:background="@color/green"
>
<ImageButton
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="NAGRAJ"
    android:id="@+id/nagraj"
    android:src="@drawable/button_sos"
    android:scaleType="fitStart"
    android:layout_alignParentTop="true"
    android:adjustViewBounds="true"
    android:background="@null"
    android:layout_marginBottom="25px"
    android:tag="nagraj"
    />

<ImageButton
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="112"
    android:id="@+id/zadzwon"
    android:layout_below="@+id/nagraj"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:src="@drawable/button_112"
    android:scaleType="fitStart"
    android:background="@null"
    android:adjustViewBounds="true"
    android:layout_marginBottom="25px"/>

<ImageButton
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="ZDJECIE"
    android:id="@+id/foto"
    android:layout_below="@+id/zadzwon"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:src="@drawable/button_foto"
    android:scaleType="fitStart"
    android:background="@null"
    android:adjustViewBounds="true"
    /> 
</RelativeLayout>

您是否尝试过垂直使用 LinearLayout 并添加 layout_weight 参数?无论屏幕大小如何,这将始终使按钮保持相同的间距。我认为这可能会有所帮助:

编辑: 新的 Google 百分比支持库可能也有帮助 - 特别是在响应式设计中 - http://www.androidauthority.com/using-the-android-percent-support-library-630715/

您不应为此使用 RelativeLayout 或 TableLayout。你应该使用带有 layout_weight.

的 LinearLayout

layout_weight 参数基本上意味着如果 2 个(或更多)布局具有相同的值,它们将获得相同数量的屏幕 space。在下面的代码中,所有 3 个按钮都具有相同的 layout_weight(在此示例中为 1),因此它们每个都恰好获得可用 space 的 1/3,而不会超出屏幕范围。

下面的这段代码应该可以满足您的需求。

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main"
    tools:context=".MainActivity"
    android:background="@color/green"
    android:orientation="vertical">

    <ImageButton
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="NAGRAJ"
        android:id="@+id/nagraj"
        android:src="@drawable/button_sos"
        android:background="@null"
        android:layout_marginBottom="14dp"
        android:tag="nagraj"/>

    <ImageButton
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="112"
        android:id="@+id/zadzwon"
        android:src="@drawable/button_112"
        android:background="@null"
        android:layout_marginBottom="14dp"/>

    <ImageButton
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="ZDJECIE"
        android:id="@+id/foto"
        android:src="@drawable/button_foto"
        android:background="@null"/>

</LinearLayout>

像这样同时使用 RelativeLayout 和 LinearLayout

RelativeLayout

   LinearLayout (orientation:vertical && weightsum = 3)

      ImageView (weightsum = 1)

      ImageView (weightsum = 1)

      ImageView (weightsum = 1)

   LinearLayout

   Button (align to the bot of the above LinearLayout and android:layout_alignParentBottom="true")

RelativeLayout