大二下学期每日总结

今日学习了布局服务的一些基本内容:

获取LayoutInflater实例  LayoutInflater inflater=new LayoutInflater.from(this);

加载布局文件inflate(int resoure,ViewGroup,boolean ToRoot) 第一个参数为布局资源对应的id,后两个与嵌套父布局与外部容器有关,一般天null和false。

动态添加XML布局:

获取LayoutInflater对象 LayoutInflater inflater=new LayoutInflater.from(this);

加载布局 inflater.nflate(布局文件id,null,false).findViewById(...);

通过 RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams()设置相关属性并设置位置lp.addRule();

加载布局 addView(ly,lp)将lp用ly替换。

public class MainActivity2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        final LayoutInflater inflater=LayoutInflater.from(this);
        final RelativeLayout rl=(RelativeLayout)findViewById(R.id.relativeLayout);
    }
    public void onClick(View view){
        final LayoutInflater inflater=LayoutInflater.from(this);
        final RelativeLayout rl=(RelativeLayout)findViewById(R.id.relativeLayout);
        RelativeLayout ly=(RelativeLayout) inflater.inflate(R.layout.inflate,null,false).findViewById(R.id.inflate);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        lp.addRule(RelativeLayout.CENTER_IN_PARENT);
        rl.addView(ly,lp);
    }
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity2"
    android:background="@drawable/dahe"
    android:id="@+id/relativeLayout">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="34dp"
        android:layout_marginRight="34dp"
        android:layout_marginBottom="500dp"
        android:onClick="onClick"
        android:text=" ( ゜- ゜)つロ 乾杯~①"
        android:textSize="26dp"></Button>

</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/inflate"
    android:gravity="center"
    android:orientation="vertical">

    <Button
        android:id="@+id/button1"
        android:layout_width="306dp"
        android:layout_height="63dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="48dp"
        android:layout_marginRight="48dp"
        android:layout_marginBottom="205dp"
        android:text=" ( ゜- ゜)つロ 乾杯~①"
        android:textSize="26dp"></Button>

    <ImageView
        android:layout_width="323dp"
        android:layout_height="482dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="36dp"
        android:layout_marginRight="36dp"
        android:layout_marginBottom="155dp"
        android:src="@drawable/qidan"></ImageView>

</RelativeLayout>
原文地址:https://www.cnblogs.com/fengchuiguobanxia/p/14477398.html