android - 以编程方式将 LinearLayout 的项目设置在中心屏幕

标签 android textview android-linearlayout android-relativelayout

我正在尝试创建一个网格类型 View ,其中多个 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 类:

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;
}


}

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

最佳答案

尝试这样

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

关于android - 以编程方式将 LinearLayout 的项目设置在中心屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30451305/

相关文章:

android - fragment 中的 onBackPressed()

android - 全屏应用程序,布局按通知栏高度向下移动

java - Firebase Java Lang 内存不足错误 : GC overhead limit exceeded

java - 使用 ViewPager 和 Fragments 时无法动态更改 textView 的文本

android - 为 TextFields 设置值

android - 使 ListView 的标题 View (不是节标题)保持在顶部

android - 如何截断 TextView,在 Android Studio 中的 TextView 末尾留下一定数量的字符?

android - 在线性布局中使用 inflator 添加表格行

android - 嵌套的 LinearLayouts 不工作

android - 对于只计划使用一个屏幕的布局,仅使用 LinearLayout 是一种好的做法吗?