一个简易的新闻应用

今天我们来写一个简易的新闻应用程序,我们将用到跳转,横竖屏,碎片的知识。

首先呢,我们先创建一个文件夹,叫layout-land

这个文件夹用于存放横屏时候的布局文件

操作如下图,
在这里插入图片描述

在现在这个目录下我们是看不到我们新建的文件夹的效果的,我们需要切换到project布局下

操作如下,
在这里插入图片描述

现在,我们来创建横屏的布局代码,

只要把layout目录下的activity_main.xml复制粘贴到layout-land中,不用改名字

显示如下,

在这里插入图片描述

我们打开横屏页面,把布局改为LinearLayout,标签设置为两fragment

效果如下,
在这里插入图片描述

因为这个时候我们没有fragment,就需要建立两个fragment一个碎片叫TitleFragment ,一个碎片叫ContentFragment。

方法如下,
在这里插入图片描述

我们需要把两个碎片中的内容做个清理,把我们不需要的代码删掉,只保留onCreateView方法

如下图所示,

在这里插入图片描述

现在我们需要修改横屏界面,

打开layout-land下面的actvity_main.xml这个文件,创建属性

代码如下,

<fragment
    android:id="@+id/titlefrag"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="match_parent"
    android:name="com.example.myapplication_qsq.TitleFragment"
    tools:layout="@layout/fragment_title"
    />

<fragment
    android:id="@+id/contentAc"
    android:layout_width="0dp"
    android:layout_weight="3"
    android:layout_height="match_parent"
    android:name="com.example.myapplication_qsq.ContentFragment"
    tools:layout="@layout/fragment_content"
    />

图片如下,
在这里插入图片描述

我们进入fragment_title.xml下(标题布局中),修改属性

这里用来显示新闻标题的条目

代码如下,

<ListView
   android:id="@+id/newsList1"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   />

如下图所示,

在这里插入图片描述

我们进入fragment_content.xml下(内容布局中),修改属性,我们需要两个TextView,一个用来装内容标题,一个用来装内容里的内容

这里在内容的TestView外面加了一个ScrollView控件,这个控件,可以让新闻内容滚动。

代码如下,

<LinearLayout 	
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layout_content"
android:orientation="vertical"
tools:context=".ContentFragment">


<TextView
    android:id="@+id/contentTitle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingBottom="10dp"
    android:paddingTop="10dp"
    android:text="" />

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fadingEdge="vertical"
    android:scrollbars="vertical"
    >
<TextView
    android:id="@+id/contentContent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textMultiLine"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingBottom="10dp"
    android:paddingTop="10dp"
    android:lineSpacingMultiplier="1.2"
    android:text="" />
</ScrollView>
</LinearLayout>

如下图所示,

在这里插入图片描述

现在我们修改竖屏布局,进入layout文件夹里面的activity_main.xml下

代码如下,

<LinearLayout 		
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=".MainActivity">

<fragment
    android:id="@+id/spf1Title"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.example.myapplication_qsq.TitleFragment"
    tools:layout="@layout/fragment_title"
    />

</LinearLayout>

图片如下,
在这里插入图片描述

新建一个xml文件,命名为news_item,用于显示新闻标题条目,文件里包括一个ImageView布局(标题的图片)和TextView布局(标题的标题)

操作如下,
在这里插入图片描述

代码如下,

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:id="@+id/titleImage1"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="match_parent"
    />

<TextView
    android:id="@+id/titleTitle"
    android:layout_width="0dp"
    android:layout_weight="5"
    android:layout_height="match_parent"

    android:singleLine="true"
    android:textSize="20sp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="15dp"
    android:paddingBottom="15dp"
    />
    
</LinearLayout>

图片解释如下,
在这里插入图片描述

这时我们就需要一个自己的新闻类

新建一个News类,内容为新闻图片,新闻标题,新闻内容。生成他们的无参,有参,get和set方法

新建属性,

public class News {
	private int imageId;//新闻图片
	 private String title;//新闻标题
	 private String content;//新闻内容
}

有参,无参,get,set方法都可以生成,这里我就不上代码了

在标题碎片里,创建一个全局的新闻标题的集合List

下图所示,

在这里插入图片描述

在标题碎片里,写一个getNews( )方法,把新闻标题,内容,图片放进去(把自己需要的图片复制到mipmap文件夹里),如下图所示

代码如下,

private void getNews(){
    //创建一个新闻实例
    News news1 = new News();
    //添加图片
    news1.setImageId(R.mipmap.a1);
    //添加标题
    news1.setTitle("海南岛国际电影节闭幕");
    //添加内容
    news1.setContent("由国家电影局指导,中央广播电视总台与海南	省人民政府共同主办的第二届海南岛国际电影节
    12月8日在海南三亚圆满落幕。本届电影节历时8天,来自80多个国家和地区的1495部影片参加了首次设立的 “金椰奖”10大奖项的角逐,共有来自61个国家和地区的200多部影片参加了展映。" );
    //把新闻1放到新闻集合里去
    titleNews.add(news1);

    News news2 = new News();
    news2.setImageId(R.mipmap.a2);
    news2.setTitle("全国首条市内高铁济莱高铁开工建设");
    news2.setContent("2019年9月18日,全国首条市内高铁--济莱高铁正式开工建设,线路全长117.5公里,设计行车速度为350km/小时,建成后从济南市钢城区到济南新东站只需22.5分钟。");
    titleNews.add(news2);
}

在标题碎片里,onCreateView方法里,把新闻集合添加进视图View中,并修改其内容

如下图所示,

public View onCreateView(LayoutInflater inflater, final ViewGroup container,Bundle savedInstanceState) {
    View tf_view =  inflater.inflate(R.layout.fragment_title, container, false);
    getNews();
     return tf_view;

新建一个NewsAdapter类(适配器),继承于ArrayAdapter。加上他的构造函数,选择倒数第二个。

操作如下,
在这里插入图片描述

创建一个资源编号,resourceId;在构造函数 resourceId=resource;

代码如下,

//资源的编号
private int resourceId;

//适配器的构造函数
public NewsAdapter(@NonNull Context context, int resource, @NonNull List<News> objects) {
    super(context, resource, objects);
    resourceId = resource;
}

在NewsAdapter类(适配器)中,重载getView的方法,在里面,先声明一个条目视图,获得你点击的那条新闻。判断是否为滚动,如果不是滚动就用模板文件所对应的Id号把视图做出来,如果是滚动就用你滚出去的那一条视图。如下图所示

代码如下,

 @NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    View itemview;//条目视图
    News news = getItem(position);//获得你点击的那条新闻
    //判断是否滚动
    if(convertView == null){
        itemview = LayoutInflater.from(getContext()).inflate(resourceId,null);
    }else{
        itemview=convertView;
    }

在NewsAdapter类(适配器)中,getView中,为条目赋值。找到所对应的标题控件,和图片控件,,然后赋值给他们,最后返回此条目给系统。

如下图所示

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    View itemview;//条目视图
    News news = getItem(position);//获得你点击的那条新闻
    //判断是否滚动
    if(convertView == null){
        itemview = LayoutInflater.from(getContext()).inflate(resourceId,null);
    }else{
        itemview=convertView;
    }
    //为条目赋值
    //找到ImageView的布局给此视图
    ImageView iv = itemview.findViewById(R.id.titleImage1);
    //得到图片的id
    iv.setImageResource(news.getImageId());
    //找到TextView的布局给此视图
    TextView tv = itemview.findViewById(R.id.titleTitle);
    //得到标题
    tv.setText(news.getTitle());
    //返回此条目给系统
    return itemview;
}

该适配器给标题碎片来用TitleFragment

在标题碎片中(TitleFragment),onCreateView方法里,创建一个适配器的实例,参数有三个,第一个是场景,第二个是采用哪个模板(模板名字),第三个是采用哪个集合。

如代码如下,

public View onCreateView(LayoutInflater inflater, final ViewGroup container,Bundle savedInstanceState) {
    View tf_view =  inflater.inflate(R.layout.fragment_title, container, false);
    getNews();
    NewsAdapter na = new NewsAdapter(getContext(),R.layout.news_item,titleNews);
     return tf_view;
     }

在标题碎片中(TitleFragment),onCreateView方法里,在这个碎片视图中把ListView求出来

如下图所示

public View onCreateView(LayoutInflater inflater, final ViewGroup container,Bundle savedInstanceState) {
    View tf_view =  inflater.inflate(R.layout.fragment_title, container, false);
    getNews();
    NewsAdapter na = new NewsAdapter(getContext(),R.layout.news_item,titleNews);
     ListView listView = tf_view.findViewById(R.id.newsList1);
     return tf_view;
     }

在标题碎片中(TitleFragment),onCreateView方法里,把适配器与ListView联系起来

public View onCreateView(LayoutInflater inflater, final ViewGroup container,Bundle savedInstanceState) {
    View tf_view =  inflater.inflate(R.layout.fragment_title, container, false);
    getNews();
    NewsAdapter na = new NewsAdapter(getContext(),R.layout.news_item,titleNews);
     ListView listView = tf_view.findViewById(R.id.newsList1);
     listView.setAdapter(na);
     return tf_view;
     }

在标题碎片中(TitleFragment),建一个全局的布尔变量,他的真假代表横屏和竖屏,如下图所示(什么时候求出这个布尔值?在手机上,所有界面都运行稳定了之后,才会求)

//新闻标题
private List<News> titleNews = new ArrayList<News>();
private boolean isHengPing;

重载生命周期的方法onActivityCreated(),判断是否是横竖屏

代码如下,

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if(getActivity().findViewById(R.id.contentAc) != null){
        isHengPing=true;
    }else {
        isHengPing=false;
    }
}

创建一个内容活动,命名为ContentActivity,用它显示竖屏时的点击内容,

修改ContentActivity活动对应的布局,打开activity_content.xml,修改属性,内容碎片对应的id名与横屏中内容的id名相同,代码如下

<LinearLayout 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=".ContentActivity">

<fragment
    android:id="@+id/contentAc"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.example.myapplication_qsq.ContentFragment"
    tools:layout="@layout/fragment_content"
    />

</LinearLayout>

为ListView的条目设置点击事件,用两种方法,一种是接口的方法,一种是直接设置监听的方法,这里我们使用,直接设置监听的方法。点击之后,跳转到ContentActivity活动中去。

在点击事件中我们要有一个判断,如果是横屏你要怎么样,如果是竖屏你要怎么样。

代码如下,

@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                         Bundle savedInstanceState) {
    View tf_view =  inflater.inflate(R.layout.fragment_title, container, false);
    getNews();
    NewsAdapter na = new NewsAdapter(getContext(),R.layout.news_item,titleNews);
    ListView listView = tf_view.findViewById(R.id.newsList1);
    listView.setAdapter(na);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //得到条目里的内容
            News news = titleNews.get(position);
            if(isHengPing){
                //横屏的代码
                //获取标题的控件
                TextView title =getActivity().findViewById(R.id.contentTitle);
                //获取内容的控件
                TextView content =getActivity().findViewById(R.id.contentContent);
                //写入新闻的标题
                title.setText(news.getTitle());
                //写入新闻的内容
                content.setText(news.getContent());

            }else {
                //竖屏的代码
                //创建跳转,从MAinActivity活动中跳转到ContentActivity活动中去
                Intent intent = new Intent(getActivity(),ContentActivity.class);
                //把得到的新闻标题跳转过去
                intent.putExtra("title",news.getTitle());
                //把得到的新闻内容跳转过去
                intent.putExtra("content",news.getContent());
                //实施跳转
                startActivity(intent);
            }

        }
    });
    return tf_view;
}

这时,我们要在ContentActivity活动中,接受这个跳转的数据

代码如下,

public class ContentActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_content);
    //获取标题的控件
    TextView title = findViewById(R.id.contentTitle);
    //获取内容的控件
    TextView content = findViewById(R.id.contentContent);
    //得到电话线
    Intent intent = getIntent();
    //得到对方传过来的新闻标题
    String ti_str = intent.getStringExtra("title");
    //得到对方传过来的新闻内容
    String co_str = intent.getStringExtra("content");
    //写入到我的标题控件中
    title.setText(ti_str);
    //写入到我的内容控件中
    content.setText(co_str);
}

}

完整代码

MainActivity.java代码

package com.example.myapplication_qsq;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
	 }
}

ContentActivity.java代码

package com.example.myapplication_qsq;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class ContentActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_content);
    //获取标题的控件
    TextView title = findViewById(R.id.contentTitle);
    //获取内容的控件
    TextView content = findViewById(R.id.contentContent);
    //得到电话线
    Intent intent = getIntent();
    //得到对方传过来的新闻标题
    String ti_str = intent.getStringExtra("title");
    //得到对方传过来的新闻内容
    String co_str = intent.getStringExtra("content");
    //写入到我的标题控件中
    title.setText(ti_str);
    //写入到我的内容控件中
    content.setText(co_str);
	  }
}

ContentFragment.java代码

package com.example.myapplication_qsq;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ContentFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_content, container, false);
}


}

News.java代码

package com.example.myapplication_qsq;

/**
 * @author QiuSiQi
 * @package com.example.myapplication_qsq
 * @fileName News
* @date on 2020/3/20  16:15
* @describe TODO
 */
public class News {
private int imageId;//新闻图片
private String title;//新闻标题
private String content;//新闻内容

public News() {
}

public News(int imageId, String title, String content) {
    this.imageId = imageId;
    this.title = title;
    this.content = content;
}

public int getImageId() {
    return imageId;
}

public void setImageId(int imageId) {
    this.imageId = imageId;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getContent() {
    return content;
}

public void setContent(String content) {
    this.content = content;
}
}

NewsAdapter.java代码

package com.example.myapplication_qsq;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.List;

/**
 * @author QiuSiQi
 * @package com.example.myapplication_qsq
 * @fileName NewsAdapter
 * @date on 2020/3/20  16:46
* @describe TODO
*/
public class NewsAdapter extends ArrayAdapter<News> {

//资源的编号
private int resourceId;

//适配器的构造函数
public NewsAdapter(@NonNull Context context, int resource, @NonNull List<News> objects) {
    super(context, resource, objects);
    resourceId = resource;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    View itemview;//条目视图
    News news = getItem(position);//获得你点击的那条新闻
    //判断是否滚动
    if(convertView == null){
        itemview = LayoutInflater.from(getContext()).inflate(resourceId,null);
    }else{
        itemview=convertView;
    }
    //为条目赋值
    //找到ImageView的布局给此视图
    ImageView iv = itemview.findViewById(R.id.titleImage1);
    //得到图片的id
    iv.setImageResource(news.getImageId());
    //找到TextView的布局给此视图
    TextView tv = itemview.findViewById(R.id.titleTitle);
    //得到标题
    tv.setText(news.getTitle());
    //返回此条目给系统
    return itemview;
}

}

TitleFragment.java代码

package com.example.myapplication_qsq;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;


public class TitleFragment extends Fragment {

//新闻标题
private List<News> titleNews = new ArrayList<News>();
private boolean isHengPing;

@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                         Bundle savedInstanceState) {
    View tf_view =  inflater.inflate(R.layout.fragment_title, container, false);
    getNews();
    NewsAdapter na = new NewsAdapter(getContext(),R.layout.news_item,titleNews);
    ListView listView = tf_view.findViewById(R.id.newsList1);
    listView.setAdapter(na);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //得到条目里的内容
            News news = titleNews.get(position);
            if(isHengPing){
                //横屏的代码
                //获取标题的控件
                TextView title =getActivity().findViewById(R.id.contentTitle);
                //获取内容的控件
                TextView content =getActivity().findViewById(R.id.contentContent);
                //写入新闻的标题
                title.setText(news.getTitle());
                //写入新闻的内容
                content.setText(news.getContent());

            }else {
                //竖屏的代码
                //创建跳转,从MAinActivity活动中跳转到ContentActivity活动中去
                Intent intent = new Intent(getActivity(),ContentActivity.class);
                //把得到的新闻标题跳转过去
                intent.putExtra("title",news.getTitle());
                //把得到的新闻内容跳转过去
                intent.putExtra("content",news.getContent());
                //实施跳转
                startActivity(intent);
            }

        }
    });
    return tf_view;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if(getActivity().findViewById(R.id.contentAc) != null){
        isHengPing=true;
    }else {
        isHengPing=false;
    }
}

private void getNews(){
    //创建一个新闻实例
    News news1 = new News();
    //添加图片
    news1.setImageId(R.mipmap.a1);
    //添加标题
    news1.setTitle("海南岛国际电影节闭幕");
    //添加内容
    news1.setContent("由国家电影局指导,中央广播电视总台与海南省人民政府共同主办的第二届海南岛国际电影节12月8日在海南三亚圆满落幕。本届电影节历时8天,来自80多个国家和地区的1495部影片参加了首次设立的 “金椰奖”10大奖项的角逐,共有来自61个国家和地区的200多部影片参加了展映。" );
    //把新闻1放到新闻集合里去
    titleNews.add(news1);

    News news2 = new News();
    news2.setImageId(R.mipmap.a2);
    news2.setTitle("全国首条市内高铁济莱高铁开工建设");
    news2.setContent("2019年9月18日,全国首条市内高铁--济莱高铁正式开工建设,线路全长117.5公里,设计行车速度为350km/小时,建成后从济南市钢城区到济南新东站只需22.5分钟。");
    titleNews.add(news2);


}

}

activity_main.xml代码

<LinearLayout 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=".MainActivity">

<fragment
    android:id="@+id/spf1Title"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.example.myapplication_qsq.TitleFragment"
    tools:layout="@layout/fragment_title"
    />

</LinearLayout>

layout-land文件夹下的activity_main.xml代码

<LinearLayout 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=".MainActivity">

<fragment
    android:id="@+id/titlefrag"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="match_parent"
    android:name="com.example.myapplication_qsq.TitleFragment"
    tools:layout="@layout/fragment_title"
    />

<fragment
    android:id="@+id/contentAc"
    android:layout_width="0dp"
    android:layout_weight="3"
    android:layout_height="match_parent"
    android:name="com.example.myapplication_qsq.ContentFragment"
    tools:layout="@layout/fragment_content"
    />

</LinearLayout>

activity_content.xml代码

<LinearLayout 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=".ContentActivity">

<fragment
android:id="@+id/contentAc"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.example.myapplication_qsq.ContentFragment"
tools:layout="@layout/fragment_content"
/>

</LinearLayout>

fragment_content.xml代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layout_content"
android:orientation="vertical"
tools:context=".ContentFragment">


<TextView
    android:id="@+id/contentTitle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingBottom="10dp"
    android:paddingTop="10dp"
    android:text="" />

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fadingEdge="vertical"
    android:scrollbars="vertical"
    >
<TextView
    android:id="@+id/contentContent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textMultiLine"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingBottom="10dp"
    android:paddingTop="10dp"
    android:lineSpacingMultiplier="1.2"
    android:text="" />
</ScrollView>
</LinearLayout>

fragment_title.xml代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".TitleFragment">

   <ListView
   android:id="@+id/newsList1"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   />

</LinearLayout>

news_item.xml代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:id="@+id/titleImage1"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="match_parent"
    />

<TextView
    android:id="@+id/titleTitle"
    android:layout_width="0dp"
    android:layout_weight="5"
    android:layout_height="match_parent"

    android:singleLine="true"
    android:textSize="20sp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="15dp"
    android:paddingBottom="15dp"
    />
</LinearLayout>

到这里,所有的代码就已经结束了。你明白了嘛!

效果如下

在这里插入图片描述

发布了2 篇原创文章 · 获赞 0 · 访问量 111

猜你喜欢

转载自blog.csdn.net/weixin_46748537/article/details/105268663
今日推荐