Android Java代码动态创建页面 之 动态生成浏览页面【RelativeLayout】

代码实现效果如下:

 

实现代码如下:

xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/relative">

    <!--  置顶内容设置的视图 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/topic">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="置顶内容的标题"
            android:textSize="24sp"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="置顶内容下面的内容"
            android:textSize="14sp"/>
    </LinearLayout>

</RelativeLayout>

MainActivity.java

/**
 *   java 代码 创建 视图操作
 */
public class MainActivity extends Activity {

    private ViewGroup viewGroup;

    private int countId = 100;

    private SpecialCustomView specialCustomView = new SpecialCustomView();


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView( R.layout.layout );
        // 获取 layout 的容器
        viewGroup = (ViewGroup) findViewById( R.id.relative );

        // 获取一个 滑动视图
        ScrollView container = getContainer( R.id.topic );
        // 获取一个最大的 容器 用于滑动
        LinearLayout LinearLayoutContainer = specialCustomView.addLinearLayout(
                this,
                countId ++ ,
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                LinearLayout.VERTICAL
        );

        LinearLayoutContainer.addView( getDemo() );
        LinearLayoutContainer.addView( getDemo() );
        LinearLayoutContainer.addView( getDemo() );
        LinearLayoutContainer.addView( getDemo() );
        LinearLayoutContainer.addView( getDemo() );
        LinearLayoutContainer.addView( getDemo() );
        LinearLayoutContainer.addView( getDemo() );
        LinearLayoutContainer.addView( getDemo() );


        container.addView( LinearLayoutContainer );

        viewGroup.addView( container );
        setContentView( viewGroup );
    }

    public ScrollView getContainer( int relativePosition){
         try{
            // 最大外面的视图
             ScrollView scrollView = specialCustomView.addScrollView(
                     this,
                     countId++,
                     ViewGroup.LayoutParams.MATCH_PARENT,
                     ViewGroup.LayoutParams.WRAP_CONTENT
             );

             // 特殊设置
             RelativeLayout.LayoutParams linearLayoutParams = new RelativeLayout.LayoutParams(
                     ViewGroup.LayoutParams.MATCH_PARENT,
                     ViewGroup.LayoutParams.WRAP_CONTENT
             );
             linearLayoutParams.addRule( RelativeLayout.BELOW , relativePosition );
             scrollView.setLayoutParams( linearLayoutParams );
             // 子容器 占满父容器
             scrollView.setFillViewport( true );
             return scrollView;
         }catch ( Exception e){
         }
         return null;
    }

    public LinearLayout getDemo(){
        try {
            // 最大外面的视图
            LinearLayout linearLayout = specialCustomView.addLinearLayout(
                    this,
                    countId++,
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    LinearLayout.VERTICAL
            );
            // 标题保存组件
            TextView topicTop = specialCustomView.addTextView(
                    this,
                    countId ++,
                    "topic Top",
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT
            );
            linearLayout.addView( topicTop );
            // 图片保存组件
            ImageView imageView = specialCustomView.addImageView(
                    this,
                    countId++,
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    120
            );
            linearLayout.addView( imageView );
            // 内容保存组件
            TextView topicContent = specialCustomView.addTextView(
                    this,
                    countId++,
                    "topic content",
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT
            );
            linearLayout.addView(topicContent );

            // 阅读信息保存组件容器
            LinearLayout topicReadysInfo = specialCustomView.addLinearLayout(
                    this,
                    countId++,
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    LinearLayout.HORIZONTAL
            );
            linearLayout.addView( topicReadysInfo );
            // 标题保存组件
            TextView topicAuthor = specialCustomView.addTextView(
                    this,
                    countId++,
                    "topic Author",
                    120,
                    ViewGroup.LayoutParams.WRAP_CONTENT
            );
            topicReadysInfo.addView( topicAuthor );
            // 标题保存组件
            TextView topicInfo = specialCustomView.addTextView(
                    this,
                    countId++,
                    "topic info",
                    120,
                    ViewGroup.LayoutParams.WRAP_CONTENT
            );
            topicReadysInfo.addView( topicInfo );
            // 标题保存组件
            TextView topicSource = specialCustomView.addTextView(
                    this,
                    countId++,
                    "topic Source",
                    120,
                    ViewGroup.LayoutParams.WRAP_CONTENT
            );
            topicReadysInfo.addView( topicSource );
            return linearLayout;
        }catch ( Exception e){
        }
        return null;
    }
}

封装好的一个类 就不发了,自己慢慢琢磨,其实很简单地。

 

分享:为什么是使用 Java代码产生 视图?

     目前就是我正在做 一个页面展示的功能,由于展示内容 就是 百度app首页新闻消息一样,共同点太多,所以采用了LinearLayout + ListView 产生视图内容,但是这样如果涉及到图片,视屏方面的话,就有点问题了。

    Android 请求数据方面全部是异步操作,所以一旦 视图渲染完成,可能当图片渲染到了的时候,找不到对应的视图了,因为LinearLayout + ListView产生的视图,全部一模一样的,甚至 id 值。所以无法再次通过id进行页面修改。所以就是使用到Java代码产生视图,通过自定义视图id,可以修改界面。

 

相关函数解析:https://blog.csdn.net/weixin_43359405/article/details/102622317 文章的最后

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值