سؤال

I have a little issue with my menu. I have 3 Textviews spanning the full width of the device. Each Textview should contain text and an image above.

Problems: The image always shows against the border of my textview Beneath the textview open space is added (if I remove the image, the space below is gone too)

1

I want the icon and the text to be aligned in the center of my textview as '1'.

Current partial code: (the icon is added outside the xml so not visible in this code, and I removed some code that has nothing to do with this part)

<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>

Notice: The size of my textview is different on each device, depending on the width, the height gets matched equal to the width, making a square.

هل كانت مفيدة؟

المحلول

Solved by changing android:gravity="center_horizontal" and adding android:layout_gravity="bottom"

and adding the following code to create the icons and calculating the height to define the topside padding:

 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);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top