Android 将textview项排列在listview的中心并左对齐
我正在尝试创建如下所示的列表Android 将textview项排列在listview的中心并左对齐,android,listview,textview,Android,Listview,Textview,我正在尝试创建如下所示的列表 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white" android:orientation="horizon
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="horizontal" android:gravity="center_horizontal">
<TextView
android:id="@+id/country"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="ttttttttt "
android:textColor="@color/info_text_color"
android:textSize="22sp" />
</LinearLayout>
我需要的是每个项目都应该从相同的位置开始
我的列表项XML如下所示
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="horizontal" android:gravity="center_horizontal">
<TextView
android:id="@+id/country"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="ttttttttt "
android:textColor="@color/info_text_color"
android:textSize="22sp" />
</LinearLayout>
试试这个
textview
<TextView
android:id="@+id/country"
android:layout_width="fill_parent" //this is change
android:layout_height="wrap_content"
android:gravity="center" //this is change
android:layout_marginTop="8dp"
android:text="ttttttttt "
android:textColor="@color/info_text_color"
android:textSize="22sp" />
试试这个,你的问题就会解决
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
>
<TextView
android:id="@+id/country"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="ttttttttt "
android:centerInParent="true"
android:textColor="@color/info_text_color"
android:textSize="22sp" />
</RelativeLayout >
仔细检查一下
<TextView
android:id="@+id/country"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="ttttttttt "
android:textColor="@color/info_text_color"
android:textSize="22sp" />
您只需将这一行添加到按钮中即可。。。。这是你的相对布局
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
假设您将使用线性布局,请使用下面的方法
android:layout_gravity="center"
这是您的布局,您想添加一个以星形为中心的布局吗?或者您只是想在空的listview元素中添加一个居中的星号?@daniel很抱歉,这是一个拼写错误,必须从相同的位置开始每个项目您需要从左侧或中间设置每个项目??每个项目都在中间,但左对齐我想您需要为此设置从左侧开始的边距。。。或者给文本视图指定固定的宽度,并将重力设置为左。。。因为使用布局重心,您无法将左重心设置为具有宽度换行内容的textview。在程序包“android”中找不到属性“centerInParent”的资源标识符:(试试这个..我在其中遗漏了几个字。android:layout\u centerInParent=“true”