简体   繁体   中英

Android layout (TextView)

I'm new to android development and have some doubts with regards to layout design. 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"
With layout_height="fill_parent", the image will take all the space.

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

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.

<?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. It will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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