我希望将单元格与TableRow相匹配,就像我在XML中所做的那样。比如设定重量=1。在github上尝试了许多解决方案,但不知道为什么没有一个有效。我放置了表格的屏幕截图,希望你能得到我在这里修复的东西:带有静态标题行的TableRow
<ScrollView
android:layout_width="388dp"
android:layout_height="353dp"
android:layout_below="@+id/tbPaperPrice"
android:layout_alignParentStart="true"
android:layout_above="@+id/btnSave">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tblDataDisplay">
<TableRow>
<TextView
android:text="ID"
android:layout_width="wrap_content"
android:layout_marginTop="5px"
android:layout_marginBottom="5px"
android:layout_weight="1"
/>
<TextView
android:text="Paper Type"
android:layout_width="wrap_content"
android:layout_marginTop="5px"
android:layout_marginBottom="5px"
android:layout_weight="1"
/>
<TextView
android:text="Gram"
android:layout_width="wrap_content"
android:layout_marginTop="5px"
android:layout_marginBottom="5px"
android:layout_weight="1"
/>
<TextView
android:text="Size"
android:layout_width="wrap_content"
android:layout_marginTop="5px"
android:layout_marginBottom="5px"
android:layout_weight="1"
/>
<TextView
android:text="Count"
android:layout_width="wrap_content"
android:layout_marginTop="5px"
android:layout_marginBottom="5px"
android:layout_weight="1"
/>
<TextView
android:text="Price"
android:layout_width="wrap_content"
android:layout_marginTop="5px"
android:layout_marginBottom="5px"
android:layout_weight="1"
/>
</TableRow>
<TableRow android:id="@+id/dataRow"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TableRow>
</TableLayout>
</LinearLayout>
</ScrollView>
我尝试设置LinearLayout.LayoutParams,尝试在xml中添加TableRow,并为此设置布局params,但都不起作用。我的主布局是相对的,我在其中定义了所有活动视图项。下面是我的java代码:
do {
TableRow row = new TableRow(this);
row.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.WRAP_CONTENT));
//ID column
TextView lbID = new TextView(this);
lbID.setText(data.getString(0));
lbID.setTextColor(Color.BLACK);
lbID.setGravity(Gravity.CENTER);
row.addView(lbID);
//Paper Type column
TextView lbpType = new TextView(this);
lbpType.setText(data.getString(1));
lbpType.setTextColor(Color.BLACK);
lbpType.setGravity(Gravity.CENTER);
row.addView(lbpType);
//Gram column
TextView lbGram = new TextView(this);
lbGram.setText(data.getString(data.getColumnIndex("p_gram")));
lbGram.setTextColor(Color.BLACK);
lbGram.setGravity(Gravity.CENTER);
row.addView(lbGram);
//Size column
TextView lbSize = new TextView(this);
lbSize.setText(data.getString(data.getColumnIndex("p_size")));
lbSize.setTextColor(Color.BLACK);
lbSize.setGravity(Gravity.CENTER);
row.addView(lbSize);
//Count column
TextView lbCount = new TextView(this);
lbCount.setText(data.getString(data.getColumnIndex("p_count")));
lbCount.setTextColor(Color.BLACK);
lbCount.setGravity(Gravity.CENTER);
row.addView(lbCount);
//Price column
TextView lbPrice = new TextView(this);
lbPrice.setText(data.getString(data.getColumnIndex("p_price")));
lbPrice.setTextColor(Color.BLACK);
lbPrice.setGravity(Gravity.CENTER);
row.addView(lbPrice);
tableDisplayData.addView(row);
} while (data.moveToNext());
用下面给出的代码替换xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5px"
android:layout_marginBottom="5px"
android:layout_weight="1"
android:gravity="center"
android:text="ID" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5px"
android:layout_marginBottom="5px"
android:layout_weight="1"
android:gravity="center"
android:text="Paper Type" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5px"
android:layout_marginBottom="5px"
android:layout_weight="1"
android:gravity="center"
android:text="Gram" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5px"
android:layout_marginBottom="5px"
android:layout_weight="1"
android:gravity="center"
android:text="Size" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5px"
android:layout_marginBottom="5px"
android:layout_weight="1"
android:gravity="center"
android:text="Count" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5px"
android:layout_marginBottom="5px"
android:layout_weight="1"
android:gravity="center"
android:text="Price" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
对于动态数据,您应该使用RecycerView来显示这些数据
首先创建模型类PaperModel.java
,用于hold you papertype数据,如下所示:
public class PaperModel {
int id;
String paperType;
String gram;
String size;
String count;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPaperType() {
return paperType;
}
public void setPaperType(String paperType) {
this.paperType = paperType;
}
public String getGram() {
return gram;
}
public void setGram(String gram) {
this.gram = gram;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
public String getCount() {
return count;
}
public void setCount(String count) {
this.count = count;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
String price;
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5px"
android:layout_marginBottom="5px"
android:layout_weight="1"
android:gravity="center"
android:id="@+id/id" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5px"
android:layout_marginBottom="5px"
android:layout_weight="1"
android:gravity="center"
android:id="@+id/paper_type" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5px"
android:layout_marginBottom="5px"
android:layout_weight="1"
android:gravity="center"
android:id="@+id/gram" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5px"
android:layout_marginBottom="5px"
android:layout_weight="1"
android:gravity="center"
android:id="@+id/size" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5px"
android:layout_marginBottom="5px"
android:layout_weight="1"
android:gravity="center"
android:id="@+id/count" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5px"
android:layout_marginBottom="5px"
android:layout_weight="1"
android:gravity="center"
android:id="@+id/price" />
</LinearLayout>
</LinearLayout>
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class paperAdapter extends RecyclerView.Adapter<paperAdapter.viewHolder> {
private final ArrayList<PaperModel> paperModelArrayList;
public paperAdapter(ArrayList<PaperModel> paperModelArrayList) {
this.paperModelArrayList = paperModelArrayList;
}
@NonNull
@Override
public viewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.paper_type, parent, false);
return new viewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull viewHolder holder, int position) {
PaperModel currentItem = paperModelArrayList.get(position);
holder.id.setText(String.valueOf(currentItem.id)); // because its in Int format
holder.paperType.setText(currentItem.paperType);
holder.gram.setText(currentItem.gram);
holder.size.setText(currentItem.size);
holder.count.setText(currentItem.count);
holder.price.setText(currentItem.price);
}
@Override
public int getItemCount() {
return paperModelArrayList.size();
}
public static class viewHolder extends RecyclerView.ViewHolder {
TextView id, paperType, gram, size, count, price;
public viewHolder(@NonNull View itemView) {
super(itemView);
id = itemView.findViewById(R.id.id);
paperType = itemView.findViewById(R.id.paper_type);
gram = itemView.findViewById(R.id.gram);
size = itemView.findViewById(R.id.size);
count = itemView.findViewById(R.id.count);
price = itemView.findViewById(R.id.price);
}
}
}
private final ArrayList<PaperModel> paperModelArrayList;
do{
PaperModel currentItem = new PaperModel(data.getString(0),
data.getString(1),
data.getString(data.getColumnIndex("p_gram")),
data.getString(data.getColumnIndex("p_size")),
data.getString(data.getColumnIndex("p_count")),
data.getString(data.getColumnIndex("p_price")));
paperModelArrayList.add(currentItem);
}while(data.moveToNext());
RecyclerView recyclerView = findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
paperAdapter paperAdapter = new paperAdapter(paperModelArrayList);
recyclerView.setAdapter(paperAdapter);
paperAdapter.notifyDataSetChanged();
问题内容: 我想将String放入比给定单元格宽度更长的字符串中。如何动态设置,以便可以读取整个String?这是一个例子: 在这里,您将看到将给定单元格较长的每个文本换行的代码 谢谢,但是我想根据字符串的长度动态地实现RowHeight。我想读取单元格中的整个字符串/文本。有什么建议? 我是Java初学者,这是我的第一个问题。我很高兴得到答案。 问题答案: 使用JTextArea作为呈现组件时会
祝大家日安。 如何改变桌面的背景颜色时,聚焦?
问题内容: 我可以如下设置Firefox的代理设置。 但是我也需要设置Chrome。.有人可以帮助我怎么做吗? 谢谢拉吉 问题答案: 您可以尝试使用该类,如下所示:
我们在前端使用OpenWeb js库,它们需要.NET中间层在发生某些类型的错误时向它们发送特定的HTTP头状态代码。我试图做到这一点: 它有点半途而废。看截图:http状态码 http://zerogravpro.com/temp/pic.png 请注意,我在响应标头中实现了状态码400,但我确实需要请求标头中的400。相反,我得到了“200 OK”。我如何实现这一点? 我用于进行调用的 URL
我想做的是根据滚动的数字将Imageview设置为不同的骰子侧。
我有许多s和它们每个的下面,一个具有