我在xml中有一个tablelayout.我还在代码中添加了一些行.如何在代码中设置一些视图的column_layout?
在xml我有:
android:layout_column="3"
Run Code Online (Sandbox Code Playgroud)
如何在代码中实现相同的功能(例如TextView)?
我的 TableLayout 遇到问题。它由两列和多行组成。当第二列的 TextView 包含较长宽度的文本时,它将将该列(下面的行)中的 TextView 推离屏幕(向右)。相反,我希望它将文本保留在左侧,并在末尾用省略号限制文本。谁能看出我的 XML 有什么问题吗?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#4096cc" >
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/roundedshape"
android:layout_margin="5dp"
android:stretchColumns="1">
<TableRow>
<TextView
android:layout_column="1"
android:text="Server Name"
android:textStyle="bold"
android:padding="10dip" />
<TextView
android:id="@+id/txtServerName"
android:text=""
android:gravity="right"
android:ellipsize="end"
android:padding="10dip" />
</TableRow>
<View
android:layout_height="2dip"
android:background="#FF909090" />
<TableRow>
<TextView
android:layout_column="1"
android:text="Status"
android:textStyle="bold"
android:padding="10dip" />
<ImageView
android:id="@+id/status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:padding="10dip"
android:src="@drawable/icon" />
</TableRow>
.
.
.
</TableLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud) 我正在开发一个在TableLayout中实现上述属性的应用程序.
我想知道有谁可以帮助我并告诉我如何以编程方式而不是在布局文件中这样做?
我有一个table固定的表格布局.表中有2列.
如果我colspan=2为第一行设置,那么对于剩余的行,它显示相等的宽度.(它没有采用我指定的宽度.)
小提琴附在这里
<table style='table-layout:fixed' border="1" width="100%">
<thead>
<tr>
<th colspan='2'>10000</th>
</tr>
<tr>
<th width="5%">1066fdg</th>
<th width="95%">10</th>
</tr>
</thead>
</table>
Run Code Online (Sandbox Code Playgroud)
无论如何为剩余的行设置指定的列宽?
我目前TableLayout有3行,每行有一个孩子,a TextView.我的问题是TextViews宽度不是"match_parent"他们应该的.我已经按照不为它们定义高度和宽度的程序(如文档所述)但由于某种原因问题仍然存在.
我已经打开了hierarchyviewer并查看了值,TableRow填充了宽度,但TextView不填充行,它只是包装内容.完整的xml布局如下,有没有人有任何想法?
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:background="@drawable/textlines"
android:text="@string/text1_string"
android:textColor="#000000"
android:padding="5dp">
</TextView>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:background="@drawable/textlines"
android:text="@string/text1_string"
android:textColor="#000000"
android:padding="5dp">
</TextView>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:background="@drawable/textlines"
android:text="@string/text1_string"
android:textColor="#000000"
android:padding="5dp">
</TextView>
</TableRow>
</TableLayout>
Run Code Online (Sandbox Code Playgroud)
另外,我将包含xml文件@drawable/textlines.也许问题在那里?(textlines代码取自这里提供的关于边界问题的解决方案)我非常理解它,我认为它不应该影响我的问题,但我可能是错的.无论如何,这里是:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape
android:shape="rectangle">
<stroke android:width="1dp" android:color="#FF000000" />
<solid android:color="#FFDDDDDD" />
</shape>
</item> …Run Code Online (Sandbox Code Playgroud) 我在Android中遇到了一个问题,无论我尝试过什么,我都无法解决.
想象一个tablelayout,它有3个tablerows,每行有两个textfields.
[现在只关注"显示数据"]
我需要平均对齐每行中的列.
(意思是:r [0] c [0] .width = r [1] c [0] .width = r [2] c [0] .width等.)
但是,我在textview中有不同的字符串,其中一些很短,有些很长.
使用0dp作为宽度,使用1作为layout_weight值,并不能解决我的问题.在每行(单独)列调整大小.
[见"当前行为"]
另外,给定恒定宽度值(使用px或dp)可使所有内容保持不变.我也不想要..
我想要做的是,将所有文本字段的宽度设置为一列(在所有行中).根据具有最长文本值的textview.
如果使用tablelayout和tablerows是不可能的,是否可以使用listview?
我怎样才能做到这一点 ?
我想TableLayoutPanel在GUI上的固定区域中动态添加行.所以,如果记录数量增加,那么我想要一个垂直滚动条,帮助用户查看更多记录.为此,我设置了Property AutoScroll = true;但它不起作用.
CheckBox c = new CheckBox();
c.Text = "Han";
tableLayoutPanel1.GrowStyle = TableLayoutPanelGrowStyle.AddRows;
tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize));
this.tableLayoutPanel1.RowCount = 1; this.tableLayoutPanel1.Controls.Add(c, 0, 0);
tableLayoutPanel1.AutoScrollPosition = new Point(0, tableLayoutPanel1.VerticalScroll.Maximum);
this.tableLayoutPanel1.AutoScroll = true;
tableLayoutPanel1.Padding = new Padding(0, 0, SystemInformation.VerticalScrollBarWidth, 0);
Run Code Online (Sandbox Code Playgroud) 我有一个 TableLayoutPanel,它在运行时使用文本文件填充行(从文本文件中获取每一行,并将其放入包含在新行中的单元格中)。代码如下所示:
public static string UrlList= @"C:\Users\Berisha\Desktop\URLs.txt";
string[] UrlRows = System.IO.File.ReadAllLines(@UrlList);
private void InitPaths()
{
int a = 0;
int c = 1;
while (a < UrlRows.Length-1)
{
//new label
var label = new Label();
label.Dock = DockStyle.Fill;
label.AutoSize = false;
label.Text = UrlRows[a];
label.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
label.Size = new System.Drawing.Size(22, 13);
label.BackColor = System.Drawing.Color.Transparent;
TBP.Controls.Add(label, 3, c); //Add to TableLayoutPanel
a++;
c++;
}
}
Run Code Online (Sandbox Code Playgroud)
虽然我希望能够手动编辑源代码,所以我写了一个方法来删除新创建的所有内容,但似乎卡在这里,因为它不起作用:
private void clearPaths()
{
int c = UrlRows.Length - 1;
while (c <= UrlRows.Length …Run Code Online (Sandbox Code Playgroud) 以下内容TableLayout完全匹配我的要求(它填充其父级和列均匀拉伸):
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#7434a6" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="1" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#836492" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#103456" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="1" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#958453" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#756aa9" />
</TableRow>
</TableLayout>
Run Code Online (Sandbox Code Playgroud)

但是,因为我希望可以配置行数,所以我开始以编程方式创建我的布局:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Context context = getActivity();
TableLayout.LayoutParams tableParams =
new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.MATCH_PARENT);
TableRow.LayoutParams rowParams =
new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 0, 1f);
TableRow.LayoutParams itemParams …Run Code Online (Sandbox Code Playgroud) 这个问题实际上与这篇文章有关.以编程方式设置TextView的布局权重
根据答案我只需要设置TextView布局参数如下并设置stretchColumn属性,但通过添加以下代码,它使textView从表格布局中消失.
TextView tv = new TextView(v.getContext());
tv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
Run Code Online (Sandbox Code Playgroud)
所以,这是我的代码,我想动态添加一行3列,权重为35%,5%和60%.请告诉我我的代码有什么问题.
private void addTableRow(int resIdTitle, String strValue){
TableRow tr = new TableRow(this);
// Add First Column
TextView tvTitle = new TextView(this);
tvTitle.setText(resIdTitle);
tvTitle.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 0.35f));
tr.addView(tvTitle);
// Add 2nd Column
TextView tvColon = new TextView(this);
tvColon.setText(" : ");
tvColon.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 0.05f));
tr.addView(tvColon);
// Add the 3rd Column
TextView tvValue = new TextView(this);
tvValue.setText(strValue);
tvValue.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 0.60f));
tr.addView(tvValue);
// Finally add it to the table
tl.addView(tr, …Run Code Online (Sandbox Code Playgroud)