如何设置(文本)查看编程属性? [英] How to set (Text)View attributes programmatically?

查看:81
本文介绍了如何设置(文本)查看编程属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我拼命想设置单元格属性TextView的编程表内,但不能得到这个工作!每当我设置布局属性,字段根本不会出现(但没有给出任何错误或异常)。我boilded下来到这个简单的例子:

 包mmo.application.listpro;进口android.app.Activity;
进口android.os.Bundle;
进口android.widget.LinearLayout;
进口android.widget.TableLayout;
进口android.widget.TableRow;
进口android.widget.TextView;公共类测试扩展活动
{
    @覆盖
    公共无效的onCreate(最终捆绑savedInstanceState){
        super.onCreate(savedInstanceState);        TableLayout表=新TableLayout(本);
        连续的TableRow =新的TableRow(本);
        对于(字符串标签:新的String [] {字段1,字段2,FIELD3}){
            TextView的电视=新的TextView(本);
            tv.setText(标签);
// LinearLayout.LayoutParams LP =
//新LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
// LinearLayout.LayoutParams.WRAP_CONTENT);
// lp.setMargins(2,2,2,2); //左/顶/右/底部
// tv.setLayoutParams(LP);
            row.addView(电视);
        }
        table.addView(行);
        的setContentView(表);
    }
}

这将显示三个字段,但是当你取消注释五注释行,那么什么都不会出现。为什么会这样?为什么设置布局参数导致我的TextView的不出现?我卡住了!我缺少什么?

迈克尔

PS:这里的清单,如果某种灵魂迅速想尝试了这一点:

 <?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=mmo.application.listpro安卓版code =1机器人:=的versionName发展>
    <应用
        机器人:图标=@绘制/图标
        机器人:标签=@字符串/ APP_NAME
        机器人:可调试=真正的>
        <活动
            机器人:标签=测试
            机器人:名字=测试
            机器人:screenOrientation =肖像>
            &所述;意图滤光器>
                <作用机器人:名字=android.intent.action.MAIN/>
                <类机器人:名字=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
    < /用途>
    <采用-SDK机器人:targetSdkVersion =8安卓的minSdkVersion =8/>
< /清单>


解决方案

的TableRow总是强制这些值分别为MATCH_PARENT和WRAP_CONTENT 。所以,你只需要创建默认TableRow.LayoutParams,设置页边距并将其应用到TextView的

  TableRow.LayoutParams LP =新TableRow.LayoutParams()。
lp.setMargins(2,2,2,2);
row.addView(电视,LP);;

I am desperately trying to set TextView attributes of cells within a table programmatically but can't get this to work! Whenever I set layout properties, the field will simply not appear (but not give any error or exception). I boilded this down to this simple example:

package mmo.application.listpro;

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class Test extends Activity
{
    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TableLayout table = new TableLayout(this);
        TableRow row = new TableRow(this);
        for (String label: new String[] {"field1", "field2", "field3"}) {
            TextView tv = new TextView(this);
            tv.setText(label);
//          LinearLayout.LayoutParams lp = 
//              new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 
//                                            LinearLayout.LayoutParams.WRAP_CONTENT);
//          lp.setMargins(2, 2, 2, 2);  // left/top/right/bottom
//          tv.setLayoutParams(lp);
            row.addView(tv);
        }
        table.addView(row);
        setContentView(table);
    }
}

This will show three fields, but when you uncomment the five commented line, then NOTHING will appear. Why is that so? Why does setting layout parameters cause my TextView's to not appear? I'm stuck! What am I missing?

Michael

PS.: here's the manifest, if some kind soul quickly wants to try this out:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="mmo.application.listpro" android:versionCode="1" android:versionName="Development">
    <application 
        android:icon="@drawable/icon" 
        android:label="@string/app_name"
        android:debuggable="true">
        <activity
            android:label="test" 
            android:name=".Test"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-sdk android:targetSdkVersion="8" android:minSdkVersion="8" />
</manifest> 

解决方案

TableRow always enforces those values to be respectively MATCH_PARENT and WRAP_CONTENT. So you just need to create default TableRow.LayoutParams, set margins and apply it to the TextView.

TableRow.LayoutParams lp = new TableRow.LayoutParams();
lp.setMargins(2, 2, 2, 2);
row.addView(tv, lp);;

这篇关于如何设置(文本)查看编程属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆