簡體   English   中英

在線性布局的ImageView下方將TextView居中

[英]Center TextView directly below ImageView in Linear Layout

我試圖在每個圖標正下方對齊文本,盡管文本長度不同。 我希望此屏幕截圖中的所有內容都集中在此行上:

截圖

我嘗試同時使用線性和相對布局來獲得想要的結果,但沒有成功。 這段代碼是我現在擁有的,並且與我想要的最接近:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >

    <ImageView
        android:id="@+id/grid_item_image"
        android:layout_width="50dp" 
        android:layout_height="50dp"
        android:layout_marginRight="10dp"
        android:src="@drawable/football_logo" >
    </ImageView>

    <TextView 
        android:id="@+id/grid_item_label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:layout_weight="1"
        android:layout_marginTop="5dp"
        android:gravity="center"
        android:textSize="15sp">
    </TextView>
</LinearLayout>

當然,有更好的方法可以做到這一點(也許是在Textview周圍帶有LinearLayout的RelativeLayout?)。 任何幫助將不勝感激...我對此還很陌生。

如果您的圖標大小正確(例如drawable-mdpi文件夾中的〜48像素),則可以完全省去ImageView並在TextView使用android:drawableTop="@drawable/football_logo"

如果您想堅持使用嵌套布局方法,請在ImageView使用android:layout_gravity="center_horizontal"ImageView水平居中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM