【问题标题】:Android Layout- Constraint between the RelativeLayout and LinearLayout that has the same parentAndroid Layout-具有相同父级的RelativeLayout和LinearLayout之间的约束
【发布时间】:2018-10-28 14:37:23
【问题描述】:

我的 Android 应用程序中有一个布局文件,如下所示;

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/myPage"
        android:theme="@style/login"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">

           <ImageView
                android:id="@+id/myimageview"
                android:layout_width="120dp"
                android:layout_height="120dp"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="50dp"
                android:src="@drawable/mypicture"/>
            <com.myapp.RobotoTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/myimageview"
                android:layout_marginBottom="10dp"
                android:gravity="center"
                android:text="@string/mystring"
                android:textColor="#fff"
                android:textSize="12sp"
                app:rt_fontWeight="light" />

        </RelativeLayout>

       <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_alignParentBottom="true"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:orientation="vertical">

<android.support.design.widget.TextInputLayout
            android:id="@+id/password_text_input_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            app:theme="@style/loginTheme">
              <android.support.v7.widget.AppCompatEditText
                android:id="@+id/password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="sans-serif"
                android:hint="@string/password"
                android:inputType="textPassword" />
        </android.support.design.widget.TextInputLayout> 
        </LinearLayout>
    </RelativeLayout>

我想在包含 RobotoTextViewRelativeLayout 和此布局下的 LinearLayout 之间放置空间。它们在我的布局文件中处于同一级别。另外我的应用必须在不同的屏幕分辨率上运行。

它必须类似于此文本视图下的 RobotoTextViewLinearLayout 之间的约束。

我在 LinearLayout 中尝试了android:layout_marginTop,但没有成功。 RobotoTextView与LinearLayout下的文本视图重叠

【问题讨论】:

    标签: android android-layout android-linearlayout android-relativelayout


    【解决方案1】:

    将此属性添加到为其提供 id 的底部布局:

    android:id="@+id/bottom"
    

    这些到顶部布局:

    android:layout_marginBottom="10dp"
    android:layout_alignParentTop="true"
    android:layout_above="@+id/bottom"
    

    您可以将10dp 更改为您喜欢的任何内容。
    同时删除

    android:layout_weight="1"
    

    因为它只适用于LinearLayout内的视图

    【讨论】:

      【解决方案2】:

      你解释得不好,我按照你的指示尽力了。

      RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:id="@+id/myPage"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
      <RelativeLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_above="@id/linearfirst">
          <ImageView
              android:id="@+id/myimageview"
              android:layout_width="120dp"
              android:layout_height="120dp"
              android:layout_alignParentTop="true"
              android:layout_centerHorizontal="true"
              android:layout_marginTop="50dp" />
      
          <com.myapp.RobotoTextView
              android:layout_width="159dp"
              android:layout_height="51dp"
              android:layout_below="@+id/myimageview"
              android:layout_alignStart="@+id/myimageview"
              android:layout_alignLeft="@+id/myimageview"
              android:layout_alignEnd="@+id/myimageview"
              android:layout_alignRight="@+id/myimageview"
              android:layout_marginStart="-15dp"
              android:layout_marginLeft="-15dp"
              android:layout_marginTop="13dp"
              android:layout_marginEnd="-23dp"
              android:layout_marginRight="-23dp"
              android:gravity="center"
              android:text="@string/mystring"
              android:textColor="#fff"
              android:textSize="12sp"
              app:rt_fontWeight="light" />
      
      </RelativeLayout>
      
      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:id="@+id/linearfirst"
          android:layout_marginTop="10dp"
          android:layout_alignParentBottom="true"
          android:paddingLeft="16dp"
          android:paddingRight="16dp"
          android:layout_marginBottom="2dp"
          android:orientation="vertical">
      
          <android.support.design.widget.TextInputLayout
              android:id="@+id/password_text_input_layout"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_marginBottom="10dp"
              >
              <android.support.v7.widget.AppCompatEditText
                  android:id="@+id/password"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:fontFamily="sans-serif"
                  android:hint="password"
                  android:inputType="textPassword" />
          </android.support.design.widget.TextInputLayout>
      </LinearLayout>
      

      【讨论】:

        猜你喜欢
        • 2013-06-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-10
        • 2016-07-09
        相关资源
        最近更新 更多