2014-02-24 1 views
1

내 메뉴에 약간 문제가 있습니다. 3 개의 Textview가 장치의 전체 너비에 걸쳐 있습니다. 각 Textview는 위의 텍스트와 이미지를 포함해야합니다.Android textview 드로어 블 톱 맞춤

문제 : 이 이미지는 항상 추가되는 텍스트 뷰 열린 공간 아래에는 내 텍스트 뷰 의 경계에 대해 보여줍니다

1

내가 원하는 (I 이미지를 제거하면, 아래 공간도 사라) 아이콘과 텍스트가 내 textview의 가운데에 '1'로 정렬됩니다.

현재 부분적인 코드 : (아이콘이 코드에서 XML 그렇게 보이지 않는 외부의 추가, 나는이 부분과 아무 상관이 몇 가지 코드 제거)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@color/backgroundBlue" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 

<LinearLayout 
    android:id="@+id/llMenu" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:orientation="horizontal" > 

    <com.olbrecht.thescienceofwinning.SquareTextView 
     android:id="@+id/menu_ebook" 
     android:text="@string/menu_ebook" 
     android:textColor="@color/white" 
     android:layout_width="0dp" 
     android:layout_height ="fill_parent" 
     android:layout_weight="1" 
     android:gravity="center_vertical|center_horizontal" 
     android:background="@color/menuBlue" /> 

    <com.olbrecht.thescienceofwinning.SquareTextView 
     android:id="@+id/menu_library" 
     android:text="@string/menu_library" 
     android:textColor="@color/white" 
     android:layout_width="0dp" 
     android:layout_height ="fill_parent" 
     android:layout_weight="1" 
     android:gravity="center_vertical|center_horizontal" 
     android:background="@color/menuBlue" 
     android:layout_marginLeft="2dp" 
     android:layout_marginRight="2dp" /> 

    <com.olbrecht.thescienceofwinning.SquareTextView 
     android:id="@+id/menu_faq" 
     android:text="@string/menu_faq" 
     android:textColor="@color/white" 
     android:layout_width="0dp" 
     android:layout_height ="fill_parent" 
     android:layout_weight="1" 
     android:gravity="center_vertical|center_horizontal" 
     android:background="@color/menuBlue" /> 

</LinearLayout> 

</RelativeLayout> 

주의 사항 :의 크기를 내 텍스트 뷰는 각 장치마다 너비에 따라 다르며 너비가 너비와 일치하여 사각형을 만듭니다.

+0

그래서 "drawableTop"를 사용 해달라고을 " 배경 " – pskink

+0

배경은 아이콘을 텍스트 뷰의 전체 너비/높이로 확장하고 내 텍스트를 그 위에 놓을까요? + 내 배경 색상을 제거 하시겠습니까? – ZackWhite

+0

BirmapDrawable의 x 축과 y 축 중력 속성을 자동으로 채우고 싶지 않은 경우 rhis Drawable을 LayerDrawable에 배치 한 다른 하나와 함께 사용할 수 있습니다. – pskink

답변

1

android:gravity="center_horizontal"를 변경하고 android:layout_gravity="bottom"

을 추가하고 아이콘을 만들려면 다음 코드를 추가하고 위쪽 패딩 정의하는 높이를 계산하여 해결 : 바로 사용

TextView tv_ebook = (TextView)findViewById(R.id.menu_ebook); 
tv_ebook.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ebook_icon, 0, 0); 
BitmapDrawable bd_ebook=(BitmapDrawable) this.getResources().getDrawable(R.drawable.ebook_icon); 
int h_ebook = bd_ebook.getBitmap().getHeight(); 
tv_ebook.setPadding(0, h_ebook, 0, 0); 
+0

이것은 매우 해킹되었지만 잘 결정되었습니다 – pskink