充气的TextView和的LinearLayout编程 [英] Inflating TextView and LinearLayout programmatically

查看:121
本文介绍了充气的TextView和的LinearLayout编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里的问题我没有得到相同的输出结果来看在这两种情况下,我想修复案例1得到相同的输出结果的情况下2:

The problem here I don't get the same output view result in these two cases, I want to fix case 1 to get same output result as case 2:

  1. TextView的充气和的LinearLayout,然后添加的TextView到的LinearLayout编程。
  2. 添加的TextView在XML中的LinearLayout。

code和输出案例1:

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
</LinearLayout>

text_view.xml

text_view.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:textColor="#000000"
    android:text="1"
    android:textSize="20sp"
    android:background="#AAAAAA"
    android:gravity="center"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginTop="2dp"
    android:layout_marginRight="2dp"
 />

的onCreate在LayoutTestActivity.java方法

onCreate method in LayoutTestActivity.java

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    LinearLayout lt = (LinearLayout) getLayoutInflater().inflate(R.layout.main, null);
    TextView tv = (TextView) getLayoutInflater().inflate(R.layout.text_view, null);
    lt.addView(tv);
    setContentView(lt);
}

输出视图(不正确)

output view (Not Correct)

code和输出案例2:

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TextView
        android:textColor="#000000"
        android:text="1"
        android:textSize="20sp"
        android:background="#AAAAAA"
        android:gravity="center"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginTop="2dp"
        android:layout_marginRight="2dp"
     />
</LinearLayout>

的onCreate在LayoutTestActivity.java方法

onCreate method in LayoutTestActivity.java

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

输出视图(正确)

output view (Correct)

推荐答案

我不知道这有什么差别 - 但我的建议是这样的:

I'm not sure if it makes any difference - but my suggestion is this:

提供的机器人:在ID =@ + ID / LinearLayout中 -tag为的LinearLayout 的main.xml

Provide the android:id="@+id/linearLayout"-tag for the LinearLayout in your main.xml.

那么做到这一点:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    LinearLayout lt = (LinearLayout) findViewById( R.id.linearLayout );
    TextView tv = (TextView) getLayoutInflater().inflate(R.layout.text_view, null);
    lt.addView(tv);
}

您可能还需要检测是否有作为第二供给之间的的LinearLayout其中的差异参数充气的TextView 比如当:

You might also want to test if there is a difference between supplying null or your LinearLayout lt as the second parameter when inflating the TextView e.g.:

TextView tv = (TextView) getLayoutInflater().inflate(R.layout.text_view, lt);

这篇关于充气的TextView和的LinearLayout编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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