Linear layout_gravity LinearLayout中的Bottom / Top;

时间:2017-09-10 12:48:31

标签: android android-layout layout-gravity

在LinearLayout"中有数百万个问题和答案与" layout_gravity Bottom / Top相关。堆栈溢出但我还没有解决我的问题。

我想把我的EditText放在"请把我放在最顶层"在最顶部,TextView说"请把我放在最底层"在最底层。我的梦想是非常基本但我无法实现它!

任何人都可以帮助我吗?

这是我的XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:weightSum="1">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="210dp"
        android:background="@android:color/holo_green_light"
        android:gravity="center_vertical"
        android:orientation="vertical"
        android:weightSum="1">

        <EditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPersonName"
            android:text="Please place me at very top" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:background="@android:color/background_light"
            android:text="TextView"
            android:layout_weight="0.19" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="248dp"
        android:layout_weight="0.70"
        android:background="@color/colorAccent"
        android:gravity="center_vertical"
        android:orientation="vertical"
        android:weightSum="1">

        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="62dp"
            android:layout_gravity="top"
            android:text="Button" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.16"
            android:background="@android:color/background_light"
            android:text="Please place me at very bottom" />

    </LinearLayout>

</LinearLayout>

这是我的输出:

enter image description here

2 个答案:

答案 0 :(得分:0)

您已为第二个线性布局设置了重心底部,以便将视图放在底部。

首先更改此行LinearLayout:

android:gravity="center_vertical"

android:gravity="top"

在第二个LinearLayout更改此行:

android:gravity="center_vertical"

android:gravity="bottom"

注意:避免使用嵌套权重会降低性能。

答案 1 :(得分:0)

如果LinearLayout不起作用,请不要强迫自己使用它。

  1. 嵌套的LinearLayouts和权重对性能不利。

  2. RelativeLayout(或ConstraintLayout)自然意味着将事物放在ViewGroups的锚点上

  3. 我假设您希望文本和按钮以您的其他属性为中心

    glCullFace(GL_FRONT);
                glGenFramebuffers(1, &depthMapFBO);
                glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO);
                glGenTextures(1, &depthMap);
                glBindTexture(GL_TEXTURE_2D, depthMap);
                glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16, SHADOW_WIDTH, SHADOW_HEIGHT, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, NULL);
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
    
                glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, depthMap, 0);
                glDrawBuffers(1, GL_NONE);
                glReadBuffer(GL_NONE);
                glBindFramebuffer(GL_FRAMEBUFFER, 0);
                glBindTexture(GL_TEXTURE_2D, 0);
                glCullFace(GL_BACK);
    if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
        LOGI("framebuffer incomplete");
    }
    

    或者,您只需要第二个LinearLayout上的uniform mediump sampler2DShadow shadowMap; ...... float bias = 0.005; float visibility = 1.0; for (int i=0;i<4;i++){ int index = i; visibility -= 0.2*(1.0-texture( shadowMap, vec3(FragPosLightSpace.xy + poissonDisk[index]/700.0, (FragPosLightSpace.z-bias)/FragPosLightSpace.w) )); } result =light.intensity* (visibility * (diffuse + specular)); ,然后其中的所有视图都位于底部。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:orientation="vertical"
                  android:weightSum="2">
    
    
        <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:background="@android:color/holo_green_light"
                android:layout_weight="1">
    
            <EditText
                    android:id="@+id/editText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:layout_alignParentTop="true"
                    android:inputType="textPersonName"
                    android:text="Please place me at very top" />
    
            <TextView
                    android:id="@+id/textView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:background="@android:color/background_light"
                    android:text="TextView" />
    
        </RelativeLayout>
    
        <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="@android:color/holo_red_light"
                android:orientation="vertical">
    
            <Button
                    android:id="@+id/button"
                    android:layout_width="match_parent"
                    android:layout_height="62dp"
                    android:layout_centerInParent="true"
                    android:text="Button"/>
    
            <TextView
                    android:id="@+id/textView2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:background="@android:color/background_light"
                    android:text="Please place me at very bottom"/>
    
        </RelativeLayout>
    
    </LinearLayout>