[英]Android textview drawabletop alignment
I have a little issue with my menu. 我的菜单有点问题。 I have 3 Textviews spanning the full width of the device. 我有3个Textviews横跨设备的整个宽度。 Each Textview should contain text and an image above. 每个Textview均应在上方包含文本和图像。
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) 问题:图像始终显示在我的textview边框的下方。在textview开放空间下方添加了(如果我删除该图像,则下面的空间也消失了)
I want the icon and the text to be aligned in the center of my textview as '1'. 我希望图标和文本在textview的中心对齐为“ 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) 当前的部分代码:(图标被添加到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>
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. 注意:每个设备上的textview的大小是不同的,具体取决于宽度,高度等于宽度匹配,即为正方形。
Solved by changing android:gravity="center_horizontal"
and adding android:layout_gravity="bottom"
通过更改android:gravity="center_horizontal"
并添加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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.