以编程方式在中心屏幕设置 LinearLayout 项目

set items of LinearLayout at center screen programmatically

我正在尝试创建一个网格类型视图,其中 textView 框的数量将出现在屏幕中心(水平)。我现在只测试两行。 这是我的 xml 文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"
android:layout_height="match_parent"     android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"     tools:context=".MainActivity">

<View
    android:layout_width="match_parent"
    android:layout_height="10dp"
    android:background="#ff9999"
    android:id="@+id/up"/>



<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:id="@+id/left"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"></RelativeLayout>


<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/up"
android:layout_toRightOf="@+id/left"
android:layout_centerInParent="true"
android:id="@+id/r1">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/row1"
    android:orientation="horizontal"></LinearLayout></RelativeLayout>


<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/r1"
    android:layout_toRightOf="@+id/left"
    android:layout_centerInParent="true">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/row2"
    android:orientation="horizontal"></LinearLayout></RelativeLayout>


<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:id="@+id/right"
    android:layout_alignParentRight="true"></RelativeLayout>



<View
    android:layout_width="match_parent"
    android:layout_height="10dp"
    android:background="#ff9999"
    android:layout_alignParentBottom="true"/>

</RelativeLayout>

这是我的 java class:

public class MainActivity extends ActionBarActivity {


LinearLayout row1, row2;
RelativeLayout left, right, relative_row1, relative_row2;

public static int DEVICE_SCREEN_WIDTH ;
public static int DEVICE_SCREEN_HEIGHT ;

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


    DisplayMetrics dm = new DisplayMetrics();
    ((WindowManager)  getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(dm);
    DEVICE_SCREEN_WIDTH = dm.widthPixels;
    DEVICE_SCREEN_HEIGHT = dm.heightPixels;

    relative_row1 = (RelativeLayout)findViewById(R.id.r1);
    row1 = (LinearLayout) findViewById(R.id.row1);
    row2 = (LinearLayout) findViewById(R.id.row2);
    left = (RelativeLayout)findViewById(R.id.left);
    right = (RelativeLayout)findViewById(R.id.right);


    Boolean r1 = true;
    Boolean r2 = true;

    final int N = 5; // total number of textviews to add
    final int firstHalf = N / 2 + 1;
    final int secondHalf = N - firstHalf;

    final TextView[] myTextViews = new TextView[N]; // create an empty array;

    do {

        //left.getLayoutParams().width = getHorizontalRatio(50);

        for (int i = 0; i < firstHalf; i++) {
            // create a new textview
            final TextView row1TextView = new TextView(this);


            RelativeLayout.LayoutParams params =  (RelativeLayout.LayoutParams)relative_row1.getLayoutParams();
            params.addRule(RelativeLayout.CENTER_HORIZONTAL);
            relative_row1.setLayoutParams(params);

            row1TextView.setGravity(1);
            row1TextView.setHeight(getHorizontalRatio(50));
            row1TextView.setWidth(getHorizontalRatio(50));

            // set some properties of rowTextView or something
            row1TextView.setBackgroundResource(R.drawable.rounded);
            row1TextView.setText("#" + i);

            // add the textview to the linearlayout
            row1.addView(row1TextView);

            // save a reference to the textview for later
            myTextViews[i] = row1TextView;
        }

        r1 = false;
        for (int i = 0; i < secondHalf; i++) {
            // create a new textview
            final TextView row2TextView = new TextView(this);

            row2TextView.setGravity(1);
            row2TextView.setPadding(10, 30, 10, 10);
            row2TextView.setHeight(100);
            row2TextView.setWidth(100);

            // set some properties of rowTextView or something
            row2TextView.setBackgroundResource(R.drawable.rounded_selected);
            row2TextView.setText("#" + i);

            // add the textview to the linearlayout
            row2.addView(row2TextView);

            // save a reference to the textview for later
            myTextViews[i] = row2TextView;
        }
        r2 = false;
    }
    while (r1 && r2);
}



public int getHorizontalRatio(int pt){
    int ptm = (int)(((double)pt/(double)320)*DEVICE_SCREEN_WIDTH);
    if(ptm==0){
        ptm = 1;
    }
    return ptm;
}

public int getVerticalRatio(int pt){
    int ptm = (int)(((double)pt/(double)480)*DEVICE_SCREEN_HEIGHT);
    if(ptm==0){
        ptm = 1;
    }
    return ptm;
}

public float getTextRatio(int pt){
    float ptm = (float)(((double)pt/(double)320)*DEVICE_SCREEN_WIDTH);
    if(ptm==0.0f){
        ptm = 1;
    }
    return ptm;
}


}

现在正在创建文本视图,但它们未与屏幕中心(水平)对齐。我试过使用 GridLayout、setGravity 等。我错过了什么吗?请提出建议。

这样试试

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity= Gravity.CENTER_HORIZONTAL; 
row1TextView.setLayoutParams(params);