简体   繁体   English

RelativeLayout或TableLayout Android 3按钮

[英]RelativeLayout or TableLayout Android 3 buttons

I want to do something simple, 3 buttons in 1 column, responsive, like on screen. 我想做一些简单的事情,1列中的3个按钮,响应,就像屏幕上一样。

Should I create it on RelativeLayout like in my code, or TableLayout would be better idea? 我应该像在我的代码中一样在RelativeLayout上创建它,或者TableLayout会更好吗?

How to link RelativeLayout to the top and to the bottom, to be sure that last button never go out of the bottom of screen? 如何将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>

在此输入图像描述

Have you tried using LinearLayout vertically and adding layout_weight parameters? 您是否尝试过垂直使用LinearLayout并添加layout_weight参数? This will always keep buttons in the same spacing, regardless the screen size. 无论屏幕大小如何,这将始终保持按钮的间距相同。 I think this might be helpful: https://stackoverflow.com/a/4517358/4890826 我认为这可能会有所帮助: https//stackoverflow.com/a/4517358/4890826

Edit: New Google Percent Support Library might be also helpful - especially in responsive design - http://www.androidauthority.com/using-the-android-percent-support-library-630715/ 编辑:新的Google百分比支持库可能也很有帮助 - 尤其是在响应式设计中 - http://www.androidauthority.com/using-the-android-percent-support-library-630715/

You should not use either RelativeLayout or TableLayout for this. 您不应该使用RelativeLayout或TableLayout。 You should use a LinearLayout with layout_weight. 您应该使用layout_weight的LinearLayout。

The layout_weight parameter basically means that if 2 (or more) layouts have the same value, they get the same amount of screen space. layout_weight参数基本上意味着如果2个(或更多)布局具有相同的值,则它们获得相同数量的屏幕空间。 In the code below, all 3 buttons have the same layout_weight (1 in this example), so they each get exactly 1/3 of the space available, without going outside the screen. 在下面的代码中,所有3个按钮都具有相同的layout_weight(在此示例中为1),因此它们每个都可以获得可用空间的1/3,而不会超出屏幕。

This code below should give you what you need. 下面的代码应该可以满足您的需求。

<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>

Use both RelativeLayout and LinearLayout like this 像这样使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM