我不能在网格视图中使单元格的textview正确

时间:2014-09-22 07:40:05

标签: android gridview textview gravity

我所拥有的是自定义网格视图,每行包含五列,每个单元格内部都有一个文本视图,我希望此文本视图位于单元格的右侧,但它会一直显示在左侧。如此图片: enter image description here

这是我的网格视图xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:ignore="MergeRootFrame"
    android:layout_gravity="right" >

    <GridView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/myGridView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:gravity="right"
        android:layout_marginTop="60dp"        
        android:numColumns="5" 
        android:textDirection="rtl"/>


</RelativeLayout>

这是textview样式的xml文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/grid_item"
        android:layout_width="318dp"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:text="Medium Text"
        android:textAlignment="gravity"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        android:textSize="20sp" />

</LinearLayout>

这是我的主要活动:

public class MyOrderNew extends Activity {

    private TextView text;
    private GridView gridView;
    private final String[] items = new String[]{"ملاحظات","مربع المساحة","الإرتفاع","العرض","الصنف","7","8","9","10","11"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cart);



        gridView = (GridView) this.findViewById(R.id.myGridView);
        gridView.setBackgroundColor(Color.BLUE);
        gridView.setVerticalSpacing(1);
        gridView.setHorizontalSpacing(1);
        CustomGridAdapter gridAdapter = new CustomGridAdapter(MyOrderNew.this, items);
        gridView.setAdapter(gridAdapter);

有人可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

将宽度填充父级赋予文本视图并赋予权重。或布局重力权。

答案 1 :(得分:0)

不要修复Textview宽度使其成为fill_parent 机器人:layout_width = “FILL_PARENT”

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/grid_item"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:text="Medium Text"
        android:textAlignment="gravity"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        android:textSize="20sp" />

</LinearLayout>