这里的问题我没有得到相同的输出结果来看在这两种情况下,我想修复案例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:
TextView的充气和的LinearLayout,然后添加的TextView到的LinearLayout编程。 添加的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);
上一篇:指定Android项目的依赖关系(在Eclipse)关系、项目、Android、Eclipse
下一篇:在Eclipse中,我怎么能快速访问&QUOT源;机器人库项目&QUOT;从使用它的项目的源?项目、它的、机器人、快速