2016-09-13 9 views
1

私はこのような奇妙な問題を抱えています。ツールバーでテキストビューを上に移動する

イム下の画像を扱う忙しい:問題は、私はTextViewに(画像では「9」)が上方に移動するために取得する必要があるということです

Cart counter

。この画像+テキストビューはツールバーにあります。

レイアウトファイルでは、イメージを上下に動かすことはできますが、ツールバーにスペースがないため、画像が途切れることがあります。ですから、イメージを下に移動すると、カートアイコンが下に切り取られます。

私はそのカウンタを上に移動する必要があります。私はTextViewの上、以下の試してみました:

アンドロイド:paddingBottomの= "9dp"

アンドロイド:layout_marginBottom = "9dp"

どちらもの彼らはテキストビューを移動します。

の下にレイアウトファイルを参照してください。

<RelativeLayout android:id="@+id/counterPanel" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

    <ImageView 
    android:id="@+id/cartCountIcon" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/cart_dark"/> 

    <TextView 
    android:id="@+id/cartCount" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerInParent="true" 
    android:textColor="@color/orange_6" 
    tools:text="7"/> 

</RelativeLayout> 

と、これは私がコードでこのレイアウトを使用しています方法です:

public static Drawable buildCartDrawable(Context context, int count, boolean isCartLight) { 
    LayoutInflater inflater = LayoutInflater.from(context); 

    View view = inflater.inflate(R.layout.item_cart_counter_menu_item, null); 
    TextView textView = (TextView) view.findViewById(R.id.cartCount); 
    ImageView cartIcon = (ImageView) view.findViewById(R.id.cartCountIcon); 
    int backgroundImageId; 

    if (isCartLight) { 
     textView.setTextColor(ContextCompat.getColor(context, R.color.white)); 
     backgroundImageId = R.drawable.cart_empty; 
     if (count > 0) { 
     backgroundImageId = R.drawable.cart; 
     } 
    } else { 
     backgroundImageId = R.drawable.cart_empty_dark; 
     if (count > 0) { 
     backgroundImageId = R.drawable.cart_dark; 
     } 
    } 
    cartIcon.setBackgroundResource(backgroundImageId); 

    if (count == 0) { 
     textView.setVisibility(View.GONE); 
    } else if (count < 10) { 
     textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12); 
    } else { 
     textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 11); 
    } 

    textView.setText(String.valueOf(count)); 
    view.measure(
      View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), 
      View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); 
    view.layout(0, 0, cartIcon.getMeasuredWidth(), cartIcon.getMeasuredHeight()); 
    view.setDrawingCacheEnabled(true); 
    view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH); 
    Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache()); 
    view.setDrawingCacheEnabled(false); 

    return new BitmapDrawable(BobeApplication.getAppContext().getResources(), bitmap); 
    } 

私はせいぜい3/4DPを上に移動するのTextViewを必要としますそれはツールバーに集中しているようだが、私はこれについての修正を見ることができない。

+0

を追加してみてください。layout_alignParentTop =「true」を、同様に上マージンと遊ぶ、多分アイテムが上がるためにスペースを持っているdoenst – xanexpt

答えて

0

RelativeLayoutの代わりにFrameLayoutを使用します。 TextViewにするには、Android削除しようlayout_gravity =「センター」を追加し、多分marginBottom

<FrameLayout android:id="@+id/counterPanel" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

    <ImageView 
    android:id="@+id/cartCountIcon" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/cart_dark"/> 

    <TextView 
    android:id="@+id/cartCount" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:layout_marginBottom="2dp" 
    android:textColor="@color/orange_6" 
    tools:text="7"/> 

</FrameLayout> 
関連する問題