【问题标题】:Android layout (TextView)安卓布局(TextView)
【发布时间】:2012-12-12 09:39:23
【问题描述】:

我是 android 开发新手,对布局设计有一些疑问。我有以下代码,但它没有按我的意愿工作。我想让图像视图显示我拍摄的照片,并在其下方显示一个供用户输入数据的文本框。但是,此时,图像占据了整个屏幕,我找不到我的文本框。请多多指教。谢谢!

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:id="@+id/linear">

    <ImageView android:id="@+id/imageView1"
        android:layout_width="fill_parent" android:layout_height="fill_parent" 
        android:contentDescription="@string/desc"/>

    <EditText
        android:id="@+id/editTextSimple1"
        android:layout_height="wrap_content"
        android:layout_width="match_parent">
        <requestFocus />
    </EditText>

</LinearLayout>

【问题讨论】:

    标签: android android-layout android-intent android-emulator


    【解决方案1】:

    使用RelativeLayout而不是LinearLayout和使用下面的xml代码而不是你的xml代码,它会解决你的问题。

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linear"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_above="@+id/editTextSimple1"
            android:contentDescription="desc" />
    
        <EditText
            android:id="@+id/editTextSimple1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true" >
    
            <requestFocus />
        </EditText>
    
    </RelativeLayout>
    

    【讨论】:

    • 嗨 Dipak,如何增加 imageView 和 EditText 之间的间距?
    • @iceraven 设置 ImageView 的 layout_marginBottom 属性并尝试。
    【解决方案2】:

    将 ImageView 布局高度更改为 layout_height="wrap_content"
    使用 layout_height="fill_parent",图片将占据所有空间。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:id="@+id/linear">
    
        <ImageView android:id="@+id/imageView1"
            android:layout_width="fill_parent" 
    android:layout_height="wrap_content"  
            android:contentDescription="@string/desc"/>
    
        <EditText
            android:id="@+id/editTextSimple1"
            android:layout_height="wrap_content"
            android:layout_width="match_parent">
            <requestFocus />
        </EditText>
    
    </LinearLayout>
    

    【讨论】:

      【解决方案3】:

      问题是

      android:layout_height ="fill _parent"
      

      这使它占据了整个父空间,即全屏

      要么将高度设置为 dp、px 等的特定值,要么使用

      android:layout_height="wrap_content"
      

      如果视图足够小以适合屏幕

      【讨论】:

        【解决方案4】:
        <ImageView android:id="@+id/imageView1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:contentDescription="@string/desc"/>
        

        像这样给你的 ImageView 添加权重。它会起作用的。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-08-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多