图像未使用linearlayout显示在真实设备上

时间:2013-12-24 10:51:07

标签: android android-layout

我的布局分为3个,即imageview和另外2个linearlayout。对于每个线性布局,imageview的权重为2和1,总和为4.在android studio中,布局效果很好但是当我尝试在真实设备上测试时,这就是我得到的:

enter image description here

这是xml:

<?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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="20dp"
    android:weightSum="4"
    tools:context="com.newvillemovement.smartmyjob.RegisterActivity_page1">

    <ImageView
        android:layout_width="match_parent"
        android:layout_weight="2"
        android:layout_gravity="top"
        android:layout_height="match_parent"
        android:id="@+id/imageView"
        android:src="@drawable/img"
        android:scaleType="fitCenter"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/imageView"
        android:layout_weight="1"
        android:layout_centerHorizontal="true"
        android:weightSum="2"
        android:id="@+id/linearLayout">

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="Korean"
            android:id="@id/btnKorean"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:scaleType="fitCenter"
            android:onClick="btnKorean"
            android:src="@drawable/btnkoreanomal" />

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="Foreigner"
            android:id="@id/btnForeigner"
            android:layout_weight="1"
            android:scaleType="fitCenter"
            android:onClick="btnForeigner"
            android:src="@drawable/btnforeignernormal" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_weight="1"
        android:layout_alignParentBottom="true"
        android:layout_below="@+id/linearLayout"
        android:layout_marginTop="10dp"
        android:orientation="vertical">

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:text="Events"
            android:layout_weight="1"
            android:id="@+id/button3"
            android:src="@drawable/img_event_title"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:scaleType="fitCenter"
            android:layout_alignParentTop="true" />

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:id="@+id/imageView"
            android:src="@drawable/txt_event"
            android:layout_centerHorizontal="true"
            android:layout_below="@+id/button3"
            android:scaleType="fitCenter"
            android:layout_marginRight="20dp"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="10dp" />

        <ImageView
            android:id="@+id/footertext"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_centerHorizontal="true"
            android:layout_alignParentBottom="true"
            android:gravity="center"
            android:text="About About About About About About About About About About About About About"
            android:src="@drawable/footer"
            android:scaleType="fitCenter"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp" />

    </LinearLayout>

</LinearLayout>

4 个答案:

答案 0 :(得分:2)

ImageView和LinearLayouts的layout_height 必须 0dp !因此它可以修复重量,现在就试试。

答案 1 :(得分:2)

  <?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_width="match_parent"
   android:layout_height="match_parent"
   android:layout_margin="20dp"
   android:orientation="vertical" >

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:src="@drawable/abc" />

<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/imageView"
    android:layout_centerHorizontal="true"
    android:layout_gravity="center" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp"
        android:layout_weight="1"
        android:onClick="btnKorean"
        android:text="Korean" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="btnForeigner"
        android:text="Foreigner" />
</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/linearLayout"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:text="Events" />

    <Button
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_below="@+id/button3"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="5dp"
        android:layout_weight="1"
        android:text="submit" />

    <Button
        android:id="@+id/footertext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:gravity="center"
        android:text="About About About About About About About About About About  About" />
</LinearLayout>

答案 2 :(得分:0)

除了提供重量之外,您还可以使用包装内容作为所有控件的高度和宽度。这样可以。

答案 3 :(得分:0)

您需要在不同尺寸的设备中绘制不同的布局
在res

中创建布局文件夹
  1. 布局XLARGE
  2. 布局大
  3. 布局正常
  4. 布局小
  5. 并在不同大小的布局文件夹中创建相同的xml,以便您可以自定义布局 不同尺寸的设备

相关问题