【问题标题】:How can i center textviews in a table row that is constantly being generated dynamically?如何将文本视图居中在不断动态生成的表格行中?
【发布时间】:2020-08-04 13:30:38
【问题描述】:

我有一个区域列表。我的函数为每个区域创建一个表格行,并将区域名称的文本视图添加到该行,然后将此行添加到我的 TableLayout。由于 zonelist 是从数据库中填充的,因此该列表可能会在应用程序的生命周期内发生变化。 我想要做的是将每行中的每个文本视图水平居中。我正在检查每个来源,人们一直在说“将这段代码添加到您的 XML 文件中......”。由于我是动态创建这些行和文本视图,因此我的 XML 文件中没有任何对象可以操作。所以我必须以某种方式通过在 Java 端编码来管理它。任何帮助都会得到帮助。

private void addTextView(final Zone zone) {
        TableRow row = new TableRow(DashboardFragment.this.getActivity());
        TextView textView = new TextView(DashboardFragment.this.getActivity());
        textView.setId(zone.getId());
        textView.setText(zone.getName());
        textView.setTextColor(Color.BLACK);
        textView.setTextSize(25);
        TextView seperator = new TextView(DashboardFragment.this.getActivity());
        seperator.setText("_____________________________________________________________________________________________________________________________");
        seperator.setTextSize(25);
        seperator.setTextColor(Color.BLACK);
        TableRow seperate = new TableRow(DashboardFragment.this.getActivity());
        seperate.addView(seperator);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity().getApplication(), ZoneContentsActivity.class);
                intent.putExtra("zone",zone);
                startActivity(intent);
            }
        });
        row.addView(textView);
        layout.addView(row);
        layout.addView(seperate);
    }

(注意:我知道我分开的方式不是最好的 xD。所以在这方面的任何帮助都会受到重视。)

【问题讨论】:

    标签: java android xml android-studio android-layout


    【解决方案1】:

    试试这个:

    在布局文件夹中,(layout_row.xml)

    <?xml version="1.0" encoding="utf-8"?>
    <TableRow xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <LinearLayout android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical">
            <TextView
                android:id="@+id/nameTxt"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TextView"
                android:gravity="center" />
            <include layout="@layout/line_horizontal_gray" />
        </LinearLayout>
    
    </TableRow>
    

    以及将行添加到表格布局:

    private void addRow(final Zone zone){
            LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            TableRow rowView = (TableRow)inflater.inflate(R.layout.layout_row, null);
    
            TextView textView = rowView.findViewById(R.id.nameTxt);
            textView.setText(zone.getName());
            textView.setTextColor(Color.BLACK);
            textView.setTextSize(25);
    
            layout.addView(rowView);
        }
    

    在 layout_row 上添加了分隔符。希望对您有所帮助。

    line_horizo​​ntal_gray.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@android:color/darker_gray">
    
    </LinearLayout>
    

    【讨论】:

    • 非常感谢。这正是我想要的。赞赏!
    【解决方案2】:

    你可以试试

    textView.setGravity(Gravity.CENTER_HORIZONTAL);
    

    【讨论】:

    • 不幸的是,当我尝试这条线时,所有的行都是空行
    猜你喜欢
    • 2011-07-29
    • 1970-01-01
    • 2013-01-09
    • 1970-01-01
    • 2021-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多