2016-05-31 3 views
0

ImageViewとTextViewを整列しようとしましたが、imageViewが私のtextviewよりも高くなっています。ImageViewとTextViewを線形レイアウトに整列する

のXml:

  <RelativeLayout 
      android:layout_marginTop="8dp" 
      android:id="@+id/secondRelative" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/firstRelative" 
      android:orientation="horizontal"> 

      <ImageView 
       android:id="@+id/imageNote" 
       android:layout_width="15dp" 
       android:layout_height="15dp" 
       android:src="@drawable/rank" 
       android:layout_marginEnd="2dp" 
       /> 
      <TextView 
       android:id="@+id/note" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:textAlignment="center" 
       android:text="Test" 
       android:layout_alignBaseline="@id/imageNote" 
       /> 

     </RelativeLayout> 

私はプレビューに乗る:

Android preview

そして、私は私のデバイスに乗る:

Not align element

私はImageViewのを整列しますとTextViewが、私は知らない実行する方法。


編集:@Bob Maloogaと@Rethinavel Pillaiさんへ

感謝。

私はTextViewのsetCompoundDrawablesを使用しましたが、それは完璧に機能します! (それはperfの方が良い)。 TextViewに描画可能に設定するには

<RelativeLayout 
     android:id="@+id/relativeDetails" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="10dp" 
     android:layout_weight="1" 
     android:layout_marginStart="10dp" 
     android:layout_marginTop="10dp" 
     android:orientation="horizontal"> 
      <TextView 
       android:id="@+id/time" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:textAlignment="center" 
       /> 
      <TextView 
       android:id="@+id/note" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:textAlignment="center" 
       android:layout_below="@+id/time" 
       /> 
      <TextView 
       android:id="@+id/internetNeeded" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:textAlignment="center" 
       android:layout_below="@+id/note" 
       /> 
    </RelativeLayout> 

  TextView duree = (TextView) convertView.findViewById(R.id.time); 
      int colorG = Color.parseColor("#FFA500"); 
      Float taille = Utils.convertDpInPx(16, activity); 
      /*Convert DP in Pixel 
       public static float convertDpInPx(int dp, Activity activity) { 
        Resources r = activity.getResources(); 
        float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics()); 
        return px; 
       }*/ 
      Drawable imgChrono = getContext().getResources().getDrawable(R.drawable.chrono); 

      imgChrono.setBounds(0, 0, Math.round(taille), Math.round(taille)); //To change width and height of drawable 
      imgChrono.setColorFilter(colorG, PorterDuff.Mode.SRC_ATOP); //To change color of element 
      duree.setCompoundDrawables(imgChrono, null, null, null); 
      duree.setText(duration); 
+0

使用paddingTopを... .. – sushildlh

+0

RelativeLayoutとImageViewの両方を削除してみませんか? **コンパウンドドロアブル**を使用してください。演奏にはいいですね。 –

+1

より良い**パフォーマンスのために 'TextView'のために' drawableLeft'を使用してください** [this](http://stackoverflow.com/questions/4502605/how-to-programatically-set-drawableleft-on-android-button)のように –

答えて

0

この方法を試してみてください。ここで

は私の新しいコードがあるあなたのイメージビューの

<LinearLayout 
     android:layout_marginTop="8dp" 
     android:id="@+id/secondRelative" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_vertical" 
     android:orientation="horizontal"> 

     <ImageView 
      android:id="@+id/imageNote" 
      android:layout_width="15dp" 
      android:layout_height="15dp" 
      android:src="@drawable/rank" 
      android:layout_marginEnd="2dp" 
      android:layout_marginRight="2dp"/> 
     <TextView 
      android:id="@+id/note" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Test" 
      android:layout_alignBaseline="@id/imageNote" 
     /> 

    </LinearLayout> 
関連する問題