【问题标题】:I have some problems with the XML layouts (android development).我对 XML 布局(android 开发)有一些问题。
【发布时间】:2014-12-30 18:18:58
【问题描述】:

我认为我在使用 LinearLayout 容器时遇到了一些问题。我不知道如何解决这些问题:

我是 XML 的初学者,但我认为问题出在第二个 LinearLayout 中。我希望有人可以帮助我。

代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical">
    <TextView 
        android:layout_height="match_parent"
        android:layout_width="match_parent"
    />
    **<LinearLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        andriod:orientation="horizontal" >
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
           />
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />

    </LinearLayout>**
        <Button 
            android:layout_height="match_parent"
             android:layout_width="match_parent"         

            />





</LinearLayout>

【问题讨论】:

  • 你根本没有描述你的问题......
  • 我们需要知道您希望它如何布局(最好是图像)以及它是如何布局的(最好是屏幕截图)。我知道你还不能发布图片,所以只需链接到它们

标签: android xml


【解决方案1】:

我在阅读您的 xml 文件时看到的问题是,在主 LinearLayout 内,您有 3 个元素,它们的宽度和高度属性如下:

android:layout_height="match_parent"
android:layout_width="match_parent"

这意味着您希望元素完全填充主要的LinearLayout。这是行不通的。线性布局已订购不重叠的元素(RelativeLayout 就是为了这个)。由于主LinearLayout 应该是垂直定向的,我想对于这三个元素,您需要设置属性以匹配主LinearLayout 的整个宽度并垂直包装,通过设置这些值:

android:layout_height="wrap_content"
android:layout_width="match_parent"

您应该将这些应用于第二级的TextViewLinearLayoutButton 元素。

【讨论】:

    【解决方案2】:

    如果您有重叠的图像视图,请尝试为它们添加权重和/或更改宽度以与父视图不匹配并仅包装内容。

    【讨论】:

      【解决方案3】:

      请将 andriod 更改为 android(第 14 行)

      【讨论】:

        【解决方案4】:

        您发布的代码有错字。您的修复可能就像替换行一样简单:

        andriod:orientation="horizontal"

        与:

        android:orientation="horizontal"

        【讨论】:

          【解决方案5】:

          从您发布的图片来看,我认为您可能忘记添加权重以占用线性布局中的可用空间。试试这个:

          <?xml version="1.0" encoding="utf-8"?>
          <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
              android:layout_height="match_parent"
              android:layout_width="match_parent"
              android:orientation="vertical">
              <TextView 
                  android:layout_height="wrap_content"
                  android:layout_width="match_parent"
              />
              <LinearLayout
                  android:layout_height="match_parent"
                  android:layout_width="match_parent"
                  andriod:orientation="horizontal"
                  android:layout_weight="1" >
                  <ImageView
                      android:layout_width="wrap_content"
                      android:layout_height="match_parent"
                      android:layout_weight="1"
                     />
                  <ImageView
                      android:layout_width="wrap_content"
                      android:layout_height="match_parent"
                      android:layout_weight="1"
                      />
          
              </LinearLayout>
                  <Button 
                      android:layout_height="wrap_content"
                       android:layout_width="match_parent"         
                      />    
          
          </LinearLayout>
          

          【讨论】:

            猜你喜欢
            • 2019-08-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-01-10
            • 1970-01-01
            • 1970-01-01
            • 2014-01-12
            • 2012-02-17
            相关资源
            最近更新 更多