如何在动态生成的表行中居中文本视图?

huangapple go评论70阅读模式
英文:

How can i center textviews in a table row that is constantly being generated dynamically?

问题

我有一个区域列表。我的函数为每个区域创建一个表格行,将区域名称的文本视图添加到该行,然后将该行添加到我的TableLayout中。由于区域列表是从数据库中填充的,所以在应用程序的生命周期内可能会更改列表。

我尝试的是在每行中水平居中每个文本视图。我查看了各种来源,人们不断说“在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 separate = new TableRow(DashboardFragment.this.getActivity());
    separate.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(separate);
}

(注:我知道我分隔的方式不是最佳的xD。因此,对于这一方面的任何帮助也将不胜感激。)

英文:

I have a zone list. And my function creates a table row for each zone and adds a textview of zone's name to this row and then adds this row to my TableLayout. Since the zonelist is populated from a database, the list may change during the lifetime of app.
What I'm trying to do is to center each textview in each row horizontally. I'm checking every source and people keep saying "add this piece of code to your XML file...". Since I'm creating these rows and textviews dynamically, I don't have any objects to manipulate in my XML file. So I have to somehow manage it by coding in Java side. Any help would be appriciated.

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

(Note: I know the way I seperate is not the best xD. So any help on this side would be appriciated as well.)

答案1

得分: 0

你可以尝试

textView.setGravity(Gravity.CENTER_HORIZONTAL);
英文:

You can try

textView.setGravity(Gravity.CENTER_HORIZONTAL);

答案2

得分: 0

在布局文件夹中(layout),在 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.xml 中添加了分隔符。希望这能有所帮助。

line_horizontal_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>
英文:

Try this:

In layout folder, (layout_row.xml)

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;TableRow xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;wrap_content&quot;&gt;

    &lt;LinearLayout android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;
        android:layout_weight=&quot;1&quot;
        android:orientation=&quot;vertical&quot;&gt;
        &lt;TextView
            android:id=&quot;@+id/nameTxt&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:text=&quot;TextView&quot;
            android:gravity=&quot;center&quot; /&gt;
        &lt;include layout=&quot;@layout/line_horizontal_gray&quot; /&gt;
    &lt;/LinearLayout&gt;

&lt;/TableRow&gt;

And for adding Row to table layout:

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

Seperator is added on the layout_row. Hope it might help.

line_horizontal_gray.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:orientation=&quot;vertical&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;1dp&quot;
    android:background=&quot;@android:color/darker_gray&quot;&gt;

&lt;/LinearLayout&gt;

huangapple
  • 本文由 发表于 2020年8月4日 21:30:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63248006.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定