【问题标题】:Add dynamic text to RelativeLayout将动态文本添加到 RelativeLayout
【发布时间】:2013-01-27 19:53:32
【问题描述】:

我想为我的 Android 游戏创建一个“输入你的名字”页面(用于高分),但我遇到了一些问题。 我希望它看起来像这样:

您已达到 (=enthst1)

…………这是分数…………

点! (=enthst2)

请输入您的姓名以保存您的分数! (= enthst3)

........为名称编辑文本.....

后退 (=button) ................... 回车 (=button)

但我似乎无法将分数 (int) 添加到我的 ContentView!

代码如下:

“onCreate”中的 Java:

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_enter_hs);

layo = new RelativeLayout(this);
MySco = new TextView(this) ;


Back = (Button)findViewById(R.id.enthsre1);
Back.setOnClickListener(this);

Enter =(Button)findViewById(R.id.enthsok1);
Enter.setOnClickListener(this);

Eingabe = (EditText) findViewById(R.id.editText1) ;

Texta = (TextView) findViewById(R.id.enthst1) ;
Textb = (TextView) findViewById(R.id.enthst2) ;
Textc = (TextView) findViewById(R.id.enthst3) ;

Intent intentk = getIntent();
kontro = intentk.getStringExtra("from").equals("MainActivity") ;

score = 0 ;

if(kontro == false){
score = punkteRechnen(tuleb, le0leb, le1leb, le2leb) ; //calculate the score
} else {
score = 10 ;
}

scoint = "" + score ;

MySco.setText(scoint) ;
MySco.setTextColor(Color.WHITE) ; 
MySco.setTextSize(20);

/*I know that this will throw me an IllegalStateException (the specified child already has a parent)
layo.addView(Texta) ;
layo.addView(MySco) ;
layo.addView(Textb) ;
layo.addView(Textc) ;
layo.addView(Eingabe) ;
layo.addView(Back) ;
layo.addView(Enter) ;
*/

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     android:background="@color/black"
    android:orientation="vertical"
    tools:context=".EnterHSActivity" >

    <TextView
        android:id="@+id/enthst1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="@string/enthst1a"
        android:textColor="@color/white"
        android:textColorHint="@color/white"
        android:textSize="20sp"  />

    <TextView
        android:id="@+id/enthst2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:layout_centerHorizontal="true"
        android:text="@string/enthst1b"
        android:textColor="@color/white"
        android:textColorHint="@color/white"
        android:textSize="20sp"  />

    <TextView
        android:id="@+id/enthst3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="80dp"
        android:layout_centerHorizontal="true"
        android:text="@string/enthst1c"
        android:textColor="@color/white"
        android:textColorHint="@color/white"
        android:textSize="18sp"  />

    <Button
        android:id="@+id/enthsre1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="100dp"
        android:text="@string/retour" />

    <Button
        android:id="@+id/enthsok1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="20dp"
        android:layout_marginRight="100dp"
        android:text="@string/allesklar" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/enthsre1"
        android:layout_alignRight="@+id/enthsok1"
        android:layout_below="@+id/enthst3"
        android:ems="10"
        android:inputType="text"
        android:textColor="@color/white"
        android:textColorHint="@color/white"
        android:textColorLink="@color/red" >

        <requestFocus />
    </EditText>

</RelativeLayout>

那么,如何将乐谱添加到我的布局中?

【问题讨论】:

    标签: android text textview add android-relativelayout


    【解决方案1】:

    如果视图已经是该布局的子级,则不应将视图添加到该布局。当您调用 setcontentview 时,您的 XML 布局会膨胀,所有这些视图都会创建并添加到您的视图层次结构中。

    我会将 MySco 文本视图添加到您的布局 XML 中,然后调用 findviewbyid 来获取它。我会避免动态添加视图。您始终可以创建一个隐藏的视图,然后再显示它以获得类似的效果。

    这是您的 onCreate 方法的修改/精简代码。

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_enter_hs);
    MySco = (TextView)findViewById(R.id.score);
    Intent intentk = getIntent();
    kontro = intentk.getStringExtra("from").equals("MainActivity") ;
    
    score = 0 ;
    
    if(kontro == false){
    score = punkteRechnen(tuleb, le0leb, le1leb, le2leb) ; //calculate the score
    } else {
    score = 10 ;
    }
    
    scoint = "" + score ;
    
    MySco.setText(scoint) ;
    MySco.setTextColor(Color.WHITE) ; 
    MySco.setTextSize(20);
    

    在你的布局中添加这个:

    <TextView
            android:id="@+id/score"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/white"
            android:textColorHint="@color/white"
            android:textSize="20sp"  />
    

    【讨论】:

    • 我的问题是在调用punkteRechnen()之前我不会知道确切的分数,所以我不能将它添加到layout-xml中并预先将值放入Strings.xml中。我很想使用 layout-xml,但我不知道是否可以在应用程序已经运行时向 Strings.xml 添加值。
    • 没关系。调用 punkteRechnen 后,在文本视图上调用 setText,就像您所做的一样。上面代码中的问题是乐谱文本视图尚未添加到您的布局中。让布局 .xml 和 setContentView 做所有这些困难的事情。您只需要在创建文本视图后更新文本。您需要删除分配“= new xxx”的 2 个调用,并将其替换为 findViewById。然后只需更新 findViewById 返回的视图中的文本。
    • 谢谢,谢谢,非常感谢,这正是我想做的(但不幸的是,我不知道怎么做),而且效果很好!
    【解决方案2】:

    您不能在 textA、textB 等上调用 addView,因为它们已经存在于您的 XML 文件中,并且已经是视图的子视图。相反,您应该在 XML 文件中创建一个空的 RelativeLayout,然后使用 new TextView() 创建 textA 等,然后将它们添加到布局中。

    【讨论】:

    • 我知道,由于异常,我不能这样做。但是我什么时候必须设置 ContentView?如果我把它留在原处然后添加内容,新视图将不会显示,对吗?
    【解决方案3】:

    试试这个可能对你有帮助。

    在相对布局中添加文本视图以在布局上添加分数。

    MySco.setLayoutParams(new RelativeLayout.LayoutParams(300,300);
    layo.addView(MySco);
    

    【讨论】:

    • 我认为,我必须将layo 设置为 ContentView 否则它不会显示,但如果我这样做了,其他文本视图、edittext 和按钮将不存在。
    • 首先在你的 layout.xml 文件中定义线性布局,然后在该线性布局中设置 textview。要在线性布局中添加文本视图,您可以使用我上面的代码。
    • RelativeLayout 中的另一个 LinearLayout?不幸的是,我仍然必须将layo设置为ContentView,并且必须在“Enter.setOnClickListener(this);”之前执行此操作或者它不起作用(已经尝试将“setContentView(layo);”复制到最后但它只输出一些错误)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多