英文:
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)
<?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>
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
<?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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论