我正在尝试动态创建TableRow对象并将其添加到TableLayout.该TableRow对象有2个项目,一个TextView和CheckBox.该TextView项目需要有自己的布局权重设置为1,以推动CheckBox项目的最右边.
我找不到有关如何以编程方式设置项目的布局权重的文档TextView.
我正在使用Tablerow + TextView为博客文章及其回复制作一个简单的视图.在每个TableRow中我都放了一个TextView.现在我有两个问题:
比屏幕长的文本不会自动换行为多行.是TableRow的设计吗?我已经设置了tr_content.setSingleLine(false);[更新]这已经解决,我想我应该在textView中将Fill_parent更改为Wrap_content.tr_author_time.setLayoutParams(new LayoutParams(
LayoutParams.**WRAP_CONTENT**,
LayoutParams.WRAP_CONTENT));
表格不会像ListView一样滚动.我的行数大于屏幕大小.我希望该表可以向下滚动以便像ListView一样查看.那可能吗?
这是我的代码:
TableLayout tl = (TableLayout) findViewById(R.id.article_content_table);
TextView tr_title = new TextView(this);
TextView tr_author_time = new TextView(this);
TextView tr_content = new TextView(this);
TableRow tr = new TableRow(this);
for(int i = 0; i < BlogPost.size(); i++){
try{
// add the author, time
tr = new TableRow(this);
/////////////////add author+time row
BlogPost article = mBlogPost.get(i);
tr_author_time = new TextView(this);
tr_author_time.setText(article.author+"("+
article.post_time+")");
tr_author_time.setTextColor(getResources().getColor(R.color.black));
tr_author_time.setGravity(0x03);
tr_author_time.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(tr_author_time);
tl.addView(tr,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
////////////////////// …Run Code Online (Sandbox Code Playgroud) 我试图使用可编程方式从资源(xml)将TableLayout添加到LinearLayout.
添加的TableLayout计数是动态的.它将介于1到10之间.我认为从资源xml中创建它是更好的方法.因为设计不会破裂.
怎么做?
实际上我不知道如何从XML创建新实例.
请帮忙.家伙.
提前致谢.
我想在一个TableRow中使用TextView和EditText,我希望EditText的宽度比TextView大两倍,所以我把它放在TextView区域:
android:layout_weight="1"
Run Code Online (Sandbox Code Playgroud)
在EditText区域:
android:layout_weight="2"
Run Code Online (Sandbox Code Playgroud)
但它看起来不正确:
我的整个.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:id="@+id/imageButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="8dp" >
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxHeight="150dp"
android:maxWidth="150dp"
android:scaleType="centerInside"
android:src="@drawable/add_user" />
</TableRow>
<TableRow
android:id="@+id/loginRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp" >
<TextView
android:id="@+id/loginTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/login_text_view" />
<EditText
android:id="@+id/loginEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:inputType="text" >
<requestFocus />
</EditText>
</TableRow>
<TableRow
android:id="@+id/passwordRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp" >
<TextView
android:id="@+id/passwordTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/password_text_view" />
<EditText
android:id="@+id/passwordEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:inputType="textPassword" />
</TableRow>
<TableRow
android:id="@+id/nameRow"
android:layout_width="match_parent" …Run Code Online (Sandbox Code Playgroud)