Java 将布局对象添加到视图时设置布局对象的重力
我正在尝试为充气器_layout.xml充气,并将其添加到activity_main.xml中。 有人能告诉我,在我将充气机_layout.xml的视图对象添加到activity_main.xml时,如何为其分配重力吗 充气机布局.xmlJava 将布局对象添加到视图时设置布局对象的重力,java,android,xml,android-layout,Java,Android,Xml,Android Layout,我正在尝试为充气器_layout.xml充气,并将其添加到activity_main.xml中。 有人能告诉我,在我将充气机_layout.xml的视图对象添加到activity_main.xml时,如何为其分配重力吗 充气机布局.xml <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:orien
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello I'm inflated"
android:textSize="40sp"
android:id="@+id/toBeInflated"
/>
activity_main.xml(包含列表视图)
MainActivity.java
public class MainActivity extends ListActivity implements AdapterView.OnItemClickListener {
ListView l;
String[] days = {"Sunday", "Monday", "Tuesday", "Thursday", "Friday", "Saturday", "Sunday"};
LayoutInflater inflater;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l = getListView();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_view_element, days);
l.setAdapter(adapter);
inflater = getLayoutInflater();
l.setOnItemClickListener(this);
RelativeLayout r = (RelativeLayout) findViewById(R.id.relative);
View inf = inflater.inflate(R.layout.inflater_layout,r);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
}
公共类MainActivity扩展ListActivity实现AdapterView.OnItemClickListener{
列表视图l;
字符串[]天={“星期日”、“星期一”、“星期二”、“星期四”、“星期五”、“星期六”、“星期日”};
充气机;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l=getListView();
ArrayAdapter=新的ArrayAdapter(这个,R.layout.list\u view\u元素,天);
l、 设置适配器(适配器);
充气机=充气机();
l、 SetonicClickListener(本);
RelativeLayout r=(RelativeLayout)findViewById(r.id.relative);
视图inf=充气机。充气(右布局。充气机布局,右);
}
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
}
}
这就是结果:
可以通过将
LayoutParams
添加到膨胀的视图中来实现:
//small change, attachToRoot = false:
View inf = inflater.inflate(R.layout.inflater_layout,r, false);
//LayoutParams always refer to the parent of the actual view:
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//example Rule:
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
inf.setLayoutParams(lp);
r.addView(inf) //because we haven't added it yet.
还可以查找attachToRoot=false
的解释 将TextView
在activity\u main.xml
的RelativeLayout
中设置为不可见/消失(不占用空间),并在需要时使其可见,这是一种解决方案吗?否则,您需要在java代码的TextView中添加一些LayoutParams。您能详细介绍一下如何添加LayoutParams吗?谢谢
//small change, attachToRoot = false:
View inf = inflater.inflate(R.layout.inflater_layout,r, false);
//LayoutParams always refer to the parent of the actual view:
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//example Rule:
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
inf.setLayoutParams(lp);
r.addView(inf) //because we haven't added it yet.