我尝试在运行时添加自定义视图。
package com.example.bug;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
android.view.ViewGroup root = (android.view.ViewGroup)getWindow().getDecorView().findViewById(android.R.id.content);
View v0 = this.getLayoutInflater().inflate(R.layout.custom_view, null);
View v1 = this.getLayoutInflater().inflate(R.layout.custom_view, null);
((android.widget.TextView)v0.findViewById(R.id.textView1)).setText("Text view 1");
((android.widget.TextView)v1.findViewById(R.id.textView1)).setText("Text view 2");
// I expect v1 will below v0 as we are using vertical LinearLayout.
// However, they are overlapping each others.
root.addView(v0);
root.addView(v1);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
</EditText>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
然而,我不明白。我希望第二个视图位于第一个视图的底部。但是,我得到了重叠的结果。
我做错了什么步骤吗?谢谢。
注意,如果我通过XML方式而不是Java方式添加2个自定义视图,则可以正常工作。但是我仍然不明白为什么我的Java方法不起作用?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<include layout="@layout/custom_view"/>
<include layout="@layout/custom_view"/>
</LinearLayout>
答案 0 :(得分:2)
这是因为您没有将View
添加到LinearLayout
中的activity_main.xml
,而是添加到根ViewGroup
,而不是LinearLayout
}}。直接引用您的LinearLayout
并将其添加到其中,如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/main_activity_linear_layout" >
</LinearLayout>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout root = (LinearLayout) findViewById(R.id.main_activity_linear_layout);
View v0 = this.getLayoutInflater().inflate(R.layout.custom_view, null);
View v1 = this.getLayoutInflater().inflate(R.layout.custom_view, null);
((android.widget.TextView)v0.findViewById(R.id.textView1)).setText("Text view 1");
((android.widget.TextView)v1.findViewById(R.id.textView1)).setText("Text view 2");
// I expect v1 will below v0 as we are using vertical LinearLayout.
// However, they are overlapping each others.
root.addView(v0);
root.addView(v1);
}
答案 1 :(得分:0)
我建议您在添加视图时在代码中明确指定线性布局参数(宽度和高度):
View v0 = this.getLayoutInflater().inflate(R.layout.custom_view, null);
View v1 = this.getLayoutInflater().inflate(R.layout.custom_view, null);
...
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
root.addView(v0, lp);
root.addView(v1, lp);
确保您从其他课程导入LinearLayout.LayoutParams
而不是LayoutParams
。