Android:占用整个屏幕宽度的子布局

时间:2015-07-01 07:21:57

标签: android android-layout

我有一个线性布局作为具有三个子布局的父布局,我试图通过从webservice中提取来显示子布局中的图像。

我的一些图像占据了屏幕的整个宽度,不应该发生。如何避免我的子布局占据屏幕的整个宽度?

这是我的代码

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:baselineAligned="false"
  android:layout_weight="9">

<LinearLayout
    android:id="@+id/one"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="3"
    android:gravity="left"
    android:layout_alignParentLeft="true"
    android:orientation="vertical"/>

<LinearLayout
    android:id="@+id/two"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="3"
    android:gravity="center"
    android:layout_toRightOf="@id/one"
    android:orientation="vertical"/>


   <LinearLayout
    android:id="@+id/three"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="3"
    android:gravity="right"
    android:layout_alignParentRight="true"
    android:orientation="vertical"/>

  </LinearLayout>

2 个答案:

答案 0 :(得分:0)

您已对线性布局施加了权重,因为权重适用,请将layout_width更改为 0dp

答案 1 :(得分:0)

有多种方法可以做到这一点

  1. 在布局中添加图像视图,并在dp
  2. 中指定宽度+高度

    喜欢这样

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:baselineAligned="false"
        android:layout_weight="9">
    
        <LinearLayout
            android:id="@+id/one"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:gravity="left"
            android:layout_alignParentLeft="true"
            android:orientation="vertical">
    
            <ImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:id="@+id/imageView"
                android:layout_gravity="center" />
        </LinearLayout>
    
        <LinearLayout
         .....
    
        <LinearLayout
         .....
    
    </LinearLayout>
    
    1. 使用
    2. 中的比例值
        

      ImageView.ScaleType

      CENTER
      Center the image in the view, but perform no scaling. 
      
      CENTER_CROP 
      Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or larger than the corresponding dimension of the view (minus padding). 
      
      CENTER_INSIDE
      Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or less than the corresponding dimension of the view (minus padding). 
      
      FIT_CENTER
      Scale the image using CENTER.
      
      
      FIT_END
      Scale the image using END. 
      
      FIT_START
      Scale the image using START. 
      
      FIT_XY
      Scale the image using FILL. 
      
      MATRIX 
      Scale using the image matrix when drawing. 
      

      查找更多详情here