简体   繁体   English

Android布局(TextView)

[英]Android layout (TextView)

I'm new to android development and have some doubts with regards to layout design. 我是Android开发的新手,对布局设计有一些疑问。 I have the following codes but its not working as i wanted. 我有以下代码,但它不能按我的意愿工作。 I would like to have the image view display a photo i have taken and below it, a textbox for user to input data. 我想让图像视图显示我拍摄的照片,并在其下方显示一个用于输入数据的文本框。 However, at this moment, the image takes up the whole screen and I could not locate my textbox. 但是,此时,图像占据整个屏幕,我无法找到我的文本框。 Please kindly advice. 请善意的建议。 Thanks! 谢谢!

<?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>

Change ImageView layout height to layout_height="wrap_content" 将ImageView布局高度更改为layout_height="wrap_content"
With layout_height="fill_parent", the image will take all the space. 使用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>

The problem is 问题是

android:layout_height ="fill _parent"

This makes it occupy the whole parent space Ie The whole screen 这使得它占据整个父空间即整个屏幕

Either set the height to a specific value of dp,px etc. Or use 将高度设置为特定值dp,px等。或者使用

android:layout_height="wrap_content"

If view is small enough to fit on screen 如果视图小到足以适合屏幕

Use RelativeLayout instead of LinearLayout and USe below xml code instead of your xml code, it will solve your problem. 使用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>
<ImageView android:id="@+id/imageView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:contentDescription="@string/desc"/>

Add weight to your ImageView like this. 像这样为ImageView添加重量。 It will work. 它会工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM