LinearLayout,layout_gravity无法正常工作 [英] LinearLayout, layout_gravity not working properly

查看:89
本文介绍了LinearLayout,layout_gravity无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在ScrollView中设计LinearLayout,因为在激活键盘时整个内容将被推上.

I'm trying to design a LinearLayout within a ScrollView, as the whole content will be pushed up when the keyboard will be activated.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.FET.leonardo.scurcola.NameSelection"
    android:background="#e0ab18">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:text="@string/whoMaster"
        android:textColor="@color/white"
        android:textSize="50sp"
        android:layout_marginTop="25dp"
        android:textAlignment="center"
        android:id="@+id/whoMaster"
        android:layout_gravity="top"
        android:layout_weight="5" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:text="@string/zero"
        android:textSize="50sp"
        android:textColor="@color/white"
        android:id="@+id/playersLeft"
        android:layout_gravity="center"
        android:layout_weight="3"/>

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:inputType="textPersonName"
        android:hint="@string/master"
        android:ems="10"
        android:textColorHint="@color/white"
        android:textColor="@color/white"
        android:textColorHighlight="@color/white"
        android:id="@+id/names"
        android:layout_gravity="center"
        android:layout_weight="4" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:layout_gravity="bottom"
        android:orientation="horizontal">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/next"
        android:onClick="onClick"
        android:text="@string/next"
        android:textColor="#e0ab18"
        android:layout_gravity="start|center"
        />


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/back"
        android:id="@+id/back"
        android:enabled="false"
        android:onClick="onClick"
        android:textColor="#e0ab18"
        android:layout_gravity="end|center"/>

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/finish"
        android:visibility="gone"
        android:text="@string/finish"
        android:onClick="onClick"
        android:textColor="#e0ab18"/>
    </LinearLayout>

</LinearLayout>

我不明白的是为什么第二个 LinearLayout 中的按钮没有分别出现在 start 和o end LinearLayout 的内容.

What I don't understand is why the buttons inside the second LinearLayout don't get respectively on the start and o the end of the LinearLayout.

因此,我想进行最后的设计,但是我不知道如何使用LinearLayout实现此目的:

Thus, I'd like to have this final design, but I don't know how to achieve this using a LinearLayout:

推荐答案

尝试以下代码.

您可以通过执行类似的操作来应用多个重力属性

You can apply multiple gravity attributes by doing something like

android:layout_gravity="top|center_horizontal"

要在元素的父元素中对齐元素本身,您必须使用 android:layout_gravity 来对齐元素的内容,例如在textview中的文本,请使用 android:gravity

To align the element itself in it's parent you have to use android:layout_gravity to align the content of the element, say the text in a textview, you use android:gravity

当元素之间需要额外的空间时,不必像使用 EditText 那样使元素的高度超过所需的高度.只需添加< Space> 元素即可在布局中提供轻巧的空白空间.

When you need extra space between your elements it's not necessary to give the element a more than needed height like you did with the EditText. Simply add a <Space>element to provide lightweight empty spaces in your layout.

这是我将其应用于您的代码的方式:

This is how I applied it to your code:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  xmlns:tools="http://schemas.android.com/tools"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:background="#e0ab18"
                  android:orientation="vertical"
                  android:paddingBottom="@dimen/activity_vertical_margin"
                  android:paddingLeft="@dimen/activity_horizontal_margin"
                  android:paddingRight="@dimen/activity_horizontal_margin"
                  android:paddingTop="@dimen/activity_vertical_margin"
                  tools:context="com.FET.leonardo.scurcola.NameSelection">

        <TextView
            android:id="@+id/whoMaster"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_gravity="top|center_horizontal"
            android:gravity="center_horizontal"
            android:layout_marginTop="25dp"
            android:layout_weight="5"
            android:text="@string/whoMaster"
            android:textAlignment="center"
            android:textColor="@color/white"
            android:textSize="50sp"/>

        <EditText
            android:id="@+id/names"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:ems="10"
            android:hint="@string/master"
            android:inputType="textPersonName"
            android:textColor="@color/white"
            android:textColorHighlight="@color/white"
            android:textColorHint="@color/white"/>


        <Space
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="2"/>

        <TextView
            android:id="@+id/playersLeft"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_gravity="center"
            android:layout_weight="3"
            android:text="@string/zero"
            android:textColor="@color/white"
            android:textSize="50sp"/>


        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_gravity="bottom|center_horizontal"
            android:layout_weight="2"
            android:orientation="horizontal">


            <Button
                android:id="@+id/back"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="end|center"
                android:layout_marginRight="12dp"
                android:enabled="false"
                android:onClick="onClick"
                android:text="@string/back"
                android:textColor="#e0ab18"/>

            <Button
                android:id="@+id/next"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="start|center"
                android:layout_marginLeft="12dp"
                android:onClick="onClick"
                android:text="@string/next"
                android:textColor="#e0ab18"
                />

            <Button
                android:id="@+id/finish"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:onClick="onClick"
                android:text="@string/finish"
                android:textColor="#e0ab18"
                android:visibility="gone"/>
        </LinearLayout>

    </LinearLayout>
</ScrollView>

这篇关于LinearLayout,layout_gravity无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆