【问题标题】:Nested layouts strange issue嵌套布局奇怪的问题
【发布时间】:2014-06-12 08:25:14
【问题描述】:

我有这个 xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    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.gelasoft.answeringball.MainActivity$PlaceholderFragment" >

    <ImageView
        android:id="@+id/sphereIcon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:src="@drawable/rsz_rsz_mystic" 
        android:contentDescription="@string/magicBallDescr"/>        

    <LinearLayout
    android:id="@+id/table"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >


<EditText
    android:id="@+id/launch_codes"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:hint="@string/textHint"
    android:inputType="text"
   />

    </LinearLayout>


</RelativeLayout>

如您所见,我在 RelativeLayout 中包含了一个 LinearLayout,但我看到的与我想要做的相去甚远。

现在我的视图如下:

我 3 个问题:

  1. 为什么LinearLayout 会显示在球体图标上方?它被声明为 RelativeLayout

  2. 的子代
  3. 如何将球体放置在线性布局视图上方?

  4. 如何使球体图标居中?我试过了

    android:layout_centerVertical="true" android:layout_alignParentLeft="true"

但它会将图标放在视图的中心。我希望它在顶部居中。

我知道我在这里遗漏了一小部分,但作为一个 xml 初学者,我无法发现我的错误。

【问题讨论】:

    标签: java android xml android-fragments android-activity


    【解决方案1】:

    ImageView

    使用android:layout_alignParentTop="true" 对齐父级顶部

    使用android:layout_width="match_parent" 填充父宽度

    <ImageView
        android:id="@+id/sphereIcon"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_logo" 
        android:contentDescription="@string/title_activity_settings"/>  
    

    LinearLayout

    使用android:layout_below="@+id/sphereIcon"ImageView 下创建此视图

    <LinearLayout
        android:id="@+id/table"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/sphereIcon"
        android:orientation="horizontal" >
    

    【讨论】:

      【解决方案2】:

      1.从你的 ImageView 中删除这两行`

      android:layout_centerVertical="true"
      android:layout_alignParentLeft="true"
      

      现在你的完整代码

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          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.gelasoft.answeringball.MainActivity$PlaceholderFragment" >
      
          <LinearLayout
              android:id="@+id/linearLayout1"
              android:layout_width="wrap_content"
              android:layout_height="match_parent"
              android:gravity="center"
              android:orientation="horizontal" >
      
              <ImageView
                  android:id="@+id/sphereIcon"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:contentDescription=" magicBallDescr"
                  android:src="@drawable/ic_launcher" />
      
          </LinearLayout>
      
          <LinearLayout
              android:id="@+id/table"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_alignBottom="@+id/linearLayout1"
              android:layout_alignLeft="@+id/linearLayout1"
              android:layout_marginBottom="139dp"
              android:orientation="horizontal" >
      
              <EditText
                  android:id="@+id/launch_codes"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:layout_weight="1"
                  android:hint="textHint"
                  android:inputType="text" />
          </LinearLayout>
      
      </RelativeLayout>
      

      【讨论】:

        【解决方案3】:

        您可以将它与您的字符串值和 img 资源一起使用

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            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.gelasoft.answeringball.MainActivity$PlaceholderFragment" >
        
            <ImageView
                android:id="@+id/sphereIcon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:contentDescription="magicBallDescr"
                android:src="@drawable/ic_launcher" />
        
            <LinearLayout
                android:id="@+id/table"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/sphereIcon"
                android:orientation="horizontal" >
        
                <EditText
                    android:id="@+id/launch_codes"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:hint="textHint"
                    android:inputType="text" />
            </LinearLayout>
        
        </RelativeLayout>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-03-17
          • 2020-09-10
          • 1970-01-01
          • 2020-03-29
          • 1970-01-01
          • 2014-12-09
          • 1970-01-01
          • 2011-01-29
          相关资源
          最近更新 更多