Android开发-RelativeLayout内多次引用同一个RelativeLayout,相对布局

来源:互联网 发布:计算机二级c语言好考吗 编辑:程序博客网 时间:2024/06/09 15:02
转载请注明出处:http://blog.csdn.net/iwanghang/article/details/68926629
觉得博文有用,请点赞,请评论,请关注,谢谢!~


老规矩,先上图,看个效果,如果符合你的项目或者确定你要了解的内容,再往下看吧:


没什么说的,直接看代码吧~~

activity_main.xml:(干干净净的主界面布局)

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.iwanghang.addview.MainActivity"></RelativeLayout>
add_new.xml:(简简单单的一会要引入的layout)

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:text="TextView"        android:textSize="40dp"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/textView_1" />    <Button        android:id="@+id/bt1"        android:layout_below="@id/textView_1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="123"/>    <ImageView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        app:srcCompat="@mipmap/ic_launcher"        android:layout_alignBottom="@+id/bt1"        android:layout_centerHorizontal="true"        android:layout_marginBottom="29dp"        android:id="@+id/imageView_1" /></RelativeLayout>
MainActivity.java:(所有代码都在这里了)

package com.iwanghang.addview;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.Button;import android.widget.ImageView;import android.widget.RelativeLayout;import android.widget.TextView;public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        RelativeLayout container = (RelativeLayout)findViewById(R.id.activity_main);        LayoutInflater inflater = (LayoutInflater)this.getSystemService (LAYOUT_INFLATER_SERVICE);        View view = inflater.inflate(R.layout.add_new,null);        view.setId(1);        TextView text = (TextView) view.findViewById(R.id.textView_1);        text.setText("123");        Button bt = (Button) view.findViewById(R.id.bt1);        bt.setText("qqqqqq");        ImageView iv = (ImageView) view.findViewById(R.id.imageView_1);        iv.setImageResource(R.drawable.robot1);        container.addView(view);        LayoutInflater inflater2 = (LayoutInflater)this.getSystemService (LAYOUT_INFLATER_SERVICE);        View view2 = inflater2.inflate(R.layout.add_new,null);        view2.setId(2);        TextView text2 = (TextView) view2.findViewById(R.id.textView_1);        text2.setText("456");        Button bt2 = (Button) view2.findViewById(R.id.bt1);        bt2.setText("tttttt");        ImageView iv2 = (ImageView) view2.findViewById(R.id.imageView_1);        iv2.setImageResource(R.drawable.robot2);        //container.addView(view2);        // 创建RelativeLayout.LayoutParams        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);        // 添加规则  1为tv1的id        params.addRule(RelativeLayout.BELOW,1);        params.addRule(RelativeLayout.CENTER_HORIZONTAL);        // 将tv2加入布局 传入参数params        container.addView(view2,params);        setContentView(container);    }}
有疑问吗?没有。。。那么,就写到这里。。。有问题请在评论区留言~~或者贴代码到评论区~~



转载请注明出处:http://blog.csdn.net/iwanghang/article/details/68926629



欢迎移动开发爱好者交流
沈阳或周边城市公司有意开发Android,请与我联系
联系方式

微信:iwanghang
QQ:413711276
邮箱:iwanghang@qq.com



1 0
原创粉丝点击