Android “线性布局”;中心“垂直”;
我试图在ListVIEW项的中间放置一个TeTVIEW。< /P> 因此,我创建了一个LinearLayout,用于处理圆和其中的文本视图 圆是一种形状:Android “线性布局”;中心“垂直”;,android,android-layout,Android,Android Layout,我试图在ListVIEW项的中间放置一个TeTVIEW。< /P> 因此,我创建了一个LinearLayout,用于处理圆和其中的文本视图 圆是一种形状: <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <solid android:color="@color/accentColor"></solid> </shape&
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
<solid android:color="@color/accentColor"></solid>
</shape>
我希望TextView垂直居中:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="72dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:gravity="center_vertical">
<TextView
android:id="@+id/vplan_list_item_hour"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/rectangle"
android:gravity="center"
android:text="1"
android:textSize="25sp" />
</LinearLayout>
...
</RelativeLayout>
...
它在android studio的“设计”选项卡中显示正确:
但当我运行应用程序时,它看起来是这样的:
有谁知道解决方案吗?使用形状作为背景 可绘制的shape_rounded.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners android:radius="24dp"/>
<solid android:color="#A5A5A5" />
</shape>
布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="72dp" >
<TextView
android:id="@+id/vplan_list_item_hour"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/shape_rounded"
android:gravity="center"
android:text="1"
android:layout_margin="16dp"
android:textSize="25sp" />
</RelativeLayout>
您是否尝试使用
RelativeLayout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="72dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp">
<TextView
android:id="@+id/vplan_list_item_hour"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:background="@drawable/rectangle"
android:gravity="center"
android:text="1"
android:textSize="25sp" />
...
</RelativeLayout>
</RelativeLayout>
...
我试图简化一下您的布局。我在Genymotion(包括Android 4.4)中测试了它——它的工作方式很有魅力。试试看
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="72dp">
<TextView
android:id="@+id/vplan_list_item_hour"
android:layout_centerVertical="true"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginStart="16dp"
android:background="@drawable/shape_rounded"
android:gravity="center"
android:textSize="25sp"
android:text="1" />
</RelativeLayout>
希望有帮助请尝试以下代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="72dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/vplan_list_item_hour"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/rectangle"
android:padding = "7dp"
android:gravity="center"
android:text="1"
android:textSize="25sp" />
</LinearLayout>
...
...
您可以在您的文本视图中尝试两种方法:
android:gravity=“center”
和android:layout\u gravity=“center”
<TextView
android:id="@+id/vplan_list_item_hour"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/rectangle"
android:padding = "7dp"
android:gravity="center"
android:layout_gravity="center"
android:text="1"
android:textSize="25sp" />
或者你可以为LinearLayout设置背景并再次检查textview的位置。奇怪…它在我的设备上看起来很正确这是一个模拟器:genymotion 4.4也许这就是问题所在?我不知道…你有设备来检查吗?没有,它在我的android设备上看起来是一样的,它是
textview
中的背景图像还是shape
?如果我删除线性布局,我就不能垂直居中,因为当我编辑高度以“匹配父对象”时,它不再是一个圆