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>