关注代码:
axml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px"
android:layout_centerInParent="true">
<GridView
android:id="@+id/grid_chars"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="4"
android:columnWidth="40dp"
android:horizontalSpacing="1dp"
android:paddingTop="2dp"/>
</RelativeLayout>
结果:
文字居中于左侧。我如何在中间居中?
更新
以下是C#中的代码,因为我添加了以下字母:
C#:
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
string[] chars = new string[]
{
"A", "B", "C", "D", "E",
"F", "G", "H", "I", "J",
"K", "L", "M", "N", "O",
"P", "Q", "R", "S", "T",
"U", "V", "W", "X", "Y", "Z"
};
View view = inflater.Inflate(Resource.Layout.fragment_chars, container, false);
GridView grid = (GridView)view.FindViewById(Resource.Id.grid_chars);
ArrayAdapter<string> adapter = new ArrayAdapter<string>(Activity, Android.Resource.Layout.SimpleListItem1, chars);
grid.Adapter = adapter;
return view;
}
答案 0 :(得分:1)
尝试:
android:gravity="center"
android:stretchMode="spacingWidthUniform"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
或强>
android:stretchMode="columnWidth"
答案 1 :(得分:1)
Here是SimpleListItem1
布局来源:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:minHeight="?android:attr/listPreferredItemHeightSmall" />
这里android:gravity="center_vertical"
,这就是为什么
文字居中于左侧,不居中于中间。
在您的axml中,android:layout_centerInParent="true"
只是告诉GridView
成为RelativeLayout
的中心。您需要找到GridView
的项目布局,并找到TextView
以将其gravity
设置为中心。
您需要创建名为item_layout
的新布局。
在item_layout.xaml
中,将center_vertical
更改为center
,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:minHeight="?android:attr/listPreferredItemHeightSmall" />
然后替换:
ArrayAdapter<string> adapter = new ArrayAdapter<string>(Activity, Android.Resource.Layout.SimpleListItem1, chars);
为:
ArrayAdapter<string> adapter = new ArrayAdapter<string>(Activity, Resource.Layout.item_layout, chars);
答案 2 :(得分:0)
更改商品的布局。如果是RelativeLayout,请添加代码:
android:layout_centerInParent="true"
如果是LinearLayout,请添加以下代码:
android:layout_gravity="center"
如果它只是TextView,请添加代码:
android:gravity="center"