0

我试图用比率为20%和80%的两个主要元素创建项目布局到RecyclerView。第一个元素是ImageView,第二个元素是带有一些TextView的LinearLayout。基本上,它应该是这样的:LinearLayout内部的ImageView - 错误的比例

20%   80% 
+-----+-------------------+ 
| Img | Author, Date  | 
+-----+ Title...   | 
|       | 
+-------------------------+ 

所以,我创建了如下的布局,但图像总是大于20%。

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

      <ImageView 
       android:id="@+id/image" 
       android:layout_width="0dp" 
       android:layout_weight="1" 
       android:layout_height="match_parent" 
       android:src="@drawable/image" /> 

      <LinearLayout 
       android:layout_width="0dp" 
       android:layout_weight="4" 
       android:layout_height="match_parent" 
       android:orientation="vertical"> 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal"> 

        <TextView 
         android:id="@+id/author" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" /> 

        <TextView 
         android:id="@+id/date" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" /> 

       </LinearLayout> 

       <TextView 
        android:id="@+id/title" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" /> 
      </LinearLayout> 

    </LinearLayout> 

</android.support.v7.widget.CardView> 

我想要的layout_width每一个组合,但总是一些错误的比率。

任何帮助将不胜感激。

+0

机器人:weightSum = “5” 主要布局 – Pavya

+0

@Pravin遗憾的是,即使weightSum ImageView的太大。 – rubin94

回答

0

尝试:

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:weightSum="1" 
     android:orientation="horizontal"> 

      <ImageView 
       android:id="@+id/image" 
       android:layout_width="0dp" 
       android:layout_weight=".2" 
       android:layout_height="match_parent" 
       android:src="@drawable/image" /> 

      <LinearLayout 
       android:layout_width="0dp" 
       android:layout_weight=".8" 
       android:layout_height="match_parent" 
       android:orientation="vertical"> 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal"> 

        <TextView 
         android:id="@+id/author" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" /> 

        <TextView 
         android:id="@+id/date" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" /> 

       </LinearLayout> 

       <TextView 
        android:id="@+id/title" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" /> 
      </LinearLayout> 
+0

不幸的是ImageView仍然太大。 – rubin94

0

第一件事把机器人:weightSum =“5”在父母的线性布局。其次它不只是比,但图像源也很重要的位图。在将image设置在imageview之前,您可以使用缩小图像(图像资源)。 让我知道权衡是否有效。

+0

当我设置weightSum时没有任何变化。 – rubin94

+0

可能:因为没有图像源的代码存在:它可能是有问题的图像大小尝试使用滑动并将您的图像url缩略图。你也应该根据屏幕尺寸和布局比例缩放图像。 – DevKRos

0

你缺少给weightSum父母布局。 只需在您的案例LinearLayout的主布局中添加weightSum。

<android.support.v7.widget.CardView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:weightSum="5"> 
+0

不幸的是即使weightSum ImageView太大。 – rubin94

0

到目前为止,我已经理解你想创建一个列表行,然后你需要修改布局和图像视图的高度,如下所示。

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="150dp" 
     android:orientation="horizontal"> 

     <ImageView 
      android:id="@+id/image" 
      android:layout_width="0dp" 
      android:layout_height="75dp" 
      android:layout_weight="0.2" 
      android:scaleType="centerInside" 
      android:src="@drawable/image" /> 

     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="0.8" 
      android:orientation="vertical"> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="horizontal"> 

       <TextView 
        android:id="@+id/author" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" /> 

       <TextView 
        android:id="@+id/date" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" /> 

      </LinearLayout> 

      <TextView 
       android:id="@+id/title" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" /> 
     </LinearLayout> 

    </LinearLayout> 

</android.support.v7.widget.CardView>