Andrews Experiment 4 - Use debris

Experiment 4 chips use

Experimental purposes and requirements:

(1) master: Create debris

(2) to understand: the concept qualifier

(3) master: using fragments

Experiment:

1. Open the third experiment project

2, create a horizontal screen interface

Methods: re-create the same level of a directory layout-land next to the layout directory, copy the file to the activity_list.xml layout to layout-land in

3, add a fragment DetailFragment, the details make the news pieces

4, modify the details of the layout of the news, which will be replaced with the controls DetailFragment

5, modify the list of news horizontal screen layout, will be designed as a news list on the right details

 

6, can be achieved in the AVD simulate horizontal and vertical screen test function, as long as the activity is designed with layout and layout-land two layouts, so when the portrait and landscape screen layout will be different file to load.

 

 

1, when the phone in portrait or landscape screen will load the appropriate layout files, but both use the same layout files are a class file. Modify ListActivity class code, first determine the status screen, if the horizontal screen, then click on the list will be filled with news content to the right of the debris; open the event details display news content if the vertical screen, then click on the list.

Analyzing portrait or landscape screen method:

Configuration mConfiguration = MainActivity.this.getResources().getConfiguration();

int ori = mConfiguration.orientation;

if (ori == mConfiguration.ORIENTATION_LANDSCAPE) {

Toast.makeText (MainActivity.this, "now is a cross-screen", Toast.LENGTH_LONG) .show ();

} else if (ori == mConfiguration.ORIENTATION_PORTRAIT) {

Toast.makeText (MainActivity.this, "Now is the portrait", Toast.LENGTH_LONG) .show ();

}

 

Special attention:
the presence of the front portion of the computer may not detect the presence of horizontal screen vertical screen switching, the following statement can be added in OnCreate method MainActivity placed setContentView statement:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);

 

Effect: vertical screen display

 

 

 

Horizontal screen display

 

 

 

File Directory

 

 Note: This study was greatly sensitive to the word, behind deleted, this does not affect the results

 Code:

DetailActivity

package com.example.mynews;

import androidx.appcompat.app.AppCompatActivity;

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

public class DetailActivity extends AppCompatActivity {
    private long startTime;
    private News news=new News();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        startTime=System.currentTimeMillis();
        }

    @Override
    public void onBackPressed(){
        long timeOut=System.currentTimeMillis();
        Intent intent = new Intent();
        if(timeOut - startTime >= 3000)
        {
            intent.putExtra("timeReturn",0);
            setResult(RESULT_OK,intent);
        }
        else {
            intent.putExtra("timeReturn",1);
            setResult(RESULT_OK,intent);
        }
        finish();
    }
}

DetailFragment

package com.example.mynews;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import com.example.mynews.ListActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class DetailFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle saveInstanceState) {
        View view = inflater.inflate(R.layout.detail_fragment, container, false);

        TextView title = (TextView) view.findViewById(R.id.text_view7);
        TextView source = (TextView) view.findViewById(R.id.text_view8);
        TextView time = (TextView) view.findViewById(R.id.text_view9);
        TextView content = (TextView) view.findViewById(R.id.text_view10);
        //得到活动里面传输过来的值
        String title1=getActivity().getIntent().getStringExtra("textView1");
        String time1=getActivity().getIntent().getStringExtra("textView3");
        String source1=getActivity().getIntent().getStringExtra("textView2");
        String content1=getActivity().getIntent().getStringExtra("textView10");
        //采用buddle方法后,此处应该是空值
        if(title1 == null){
            title1 = getArguments().getString("textView1");
        }
        if(time1 == null){
            time1= getArguments().getString("textView3");
        }
        if(source1 == null){
            source1 = getArguments().getString("textView2");
        }
        if(content1 == null){
            content1 = getArguments().getString("textView10");
        }
        title.setText(title1);
        source.setText(source1);
        time.setText(time1);
        content.setText(content1);
        return view;
    }


}

ListActivity

package com.example.mynews;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.mynews.News;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class ListActivity extends AppCompatActivity {
    public News[] news= new News[2];
    private TextView textView1,textView2,textView3,textView4,textView5,textView6;

    public void addNews()
    {
        news[0]=new News();
        news[0].title="谈全面依法治国"

        ;
        news[0].Time="2019/12/9";
        news[0].Source="新浪网";
        news[0].content="党的十八大以来,以同志为核心的党中央提出一系列全面依法治国新理念新思想新战略。这些新理念新思想新战略,是马克思主义法治思想中国化的最新成果,是全面依法治国的根本遵循,必须长期坚持、不断丰富发展。" +
                "今天,党建网微平台编辑整理总书记相关重要论述,供大家学习参考。";

        news[1]=new News();
        news[1].title="杨洁篪同美国国务卿通电话";
        news[1].Time="20191207";
        news[1].Source="长安网";
        news[1].content="12月7日上午,中共中央政治局委员、中央外事工作委员会办公室主任杨洁篪同美国国务卿蓬佩奥通电话。"+
                "杨洁篪强调,近来美方允许所谓“香港人权与民主法案”成法,美国会众议院通过所谓“2019年维吾尔人权政策法案”,美官员多次发表歪曲、攻击中国政治制度和内外政策的言论,这是对中国内政的粗暴干涉,严重违反国际法和国际关系基本准则,严重违背中美两国人民和国际社会的意愿。中方对此表示坚决反对和强烈谴责。\n"
        ;


        textView1=(TextView) findViewById(R.id.text_view1);
        textView2=(TextView) findViewById(R.id.text_view2);
        textView3=(TextView) findViewById(R.id.text_view3);
        textView4=(TextView) findViewById(R.id.text_view4);
        textView5=(TextView) findViewById(R.id.text_view5);
        textView6=(TextView) findViewById(R.id.text_view6);

        textView1.setText(news[0].title);
        textView2.setText(news[0].Source);
        textView3.setText(news[0].Time);
        textView4.setText(news[1].title);
        textView5.setText(news[1].Source);
        textView6.setText(news[1].Time);


    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
        setContentView(R.layout.activity_list);
        addNews();

        TextView textViewButton1 = (TextView) findViewById(R.id.text_view1);
        TextView textViewButton2 = (TextView) findViewById(R.id.text_view4);

        Configuration mConfiguration = ListActivity.this.getResources().getConfiguration();
        int ori = mConfiguration.orientation;
        if (ori == mConfiguration.ORIENTATION_LANDSCAPE)
        {
            Toast.makeText(ListActivity.this, "现在是横屏", Toast.LENGTH_LONG).show();
            textViewButton1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    FragmentManager fragmentManager=getSupportFragmentManager();
                    DetailFragment detailFragment = new DetailFragment();
                    Bundle bundle = new Bundle();
                    bundle.putString("textView1", news[0].title);
                    bundle.putString("textView2",news[0].Source);
                    bundle.putString("textView3",news[0].Time);
                    bundle.putString("textView10",news[0].content);
                    detailFragment.setArguments(bundle);
                    FragmentTransaction transaction=fragmentManager.beginTransaction();
                    transaction.replace(R.id.detail_fragment, detailFragment);
                    transaction.addToBackStack(null);
                    transaction.commit();
                }
            });
            textViewButton2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    FragmentManager fragmentManager=getSupportFragmentManager();
                    DetailFragment detailFragment = new DetailFragment();
                    //这个地方是用来传输数据到碎片中的buddle方法
                    Bundle bundle = new Bundle();
                    bundle.putString("textView1", news[1].title);
                    bundle.putString("textView2",news[1].Source);
                    bundle.putString("textView3",news[1].Time);
                    bundle.putString("textView10",news[1].content);
                    detailFragment.setArguments(bundle);
                    FragmentTransaction transaction=fragmentManager.beginTransaction();
                    //这里因为在前面已经定义了,所以直接使用
                    transaction.replace(R.id.detail_fragment,detailFragment);
                    transaction.addToBackStack(null);
                    transaction.commit();
                }
            });
        }
        else if (ori == mConfiguration.ORIENTATION_PORTRAIT)
        {
            Toast.makeText(ListActivity.this, "现在是竖屏", Toast.LENGTH_LONG).show();
            textViewButton1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(ListActivity.this,DetailActivity.class);
                    intent.putExtra("textView1",news[0].title);
                    intent.putExtra("textView2",news[0].Source);
                    intent.putExtra("textView3",news[0].Time);
                    intent.putExtra("textView10",news[0].content);
                    startActivityForResult(intent,1);


                }
            });

            textViewButton2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(ListActivity.this,DetailActivity.class);
                    intent.putExtra("textView1",news[1].title);
                    intent.putExtra("textView2",news[1].Source);
                    intent.putExtra("textView3",news[1].Time);
                    intent.putExtra("textView10",news[1].content);
                    startActivityForResult(intent,2);
                }
            });
        }


    }

    @Override
    protected  void onActivityResult(int requestCode,int resultCode,Intent data)
    {
        super.onActivityResult(requestCode,resultCode,data);
        int timerReturn = data.getIntExtra("timeReturn",2);
        switch(requestCode) {
            case 1:
                if(timerReturn == 0)
                {
                    textView1.setTextColor(Color.parseColor("#8E8E8E"));
                    textView2.setTextColor(Color.parseColor("#8E8E8E"));
                    textView3.setTextColor(Color.parseColor("#8E8E8E"));

                }
                break;
            case 2:
                if(timerReturn == 0)
                {
                    textView4.setTextColor(Color.parseColor("#8E8E8E"));
                    textView5.setTextColor(Color.parseColor("#8E8E8E"));
                    textView6.setTextColor(Color.parseColor("#8E8E8E"));

                }
                break;
        }
    }
}

News

package com.example.mynews;

public class News {
    public String title;
    public String content;

    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;
    }

    public String getSource() {
        return Source;
    }

    public void setSource(String source) {
        Source = source;
    }

    public String getTime() {
        return Time;
    }

    public void setTime(String time) {
        Time = time;
    }

    public String Source;
    public String Time;
}

 

布局文件

activity_detail.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    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=".DetailActivity">

    <fragment
        android:id="@+id/detail"
        android:name="com.example.mynews.DetailFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>



</LinearLayout>

activity_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    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=".ListActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"

        android:orientation="horizontal"
        >
        android:background="#303F9F"/>
        <ImageView
            android:id="@+id/image_view"
            android:layout_height="match_parent"
            android:layout_width="130dp"

            android:src="@drawable/img_1"
            />
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"

            >
            <TextView
                android:id="@+id/text_view1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:textSize="20dp"
                android:textColor="@android:color/black"

                />
            <TextView
                android:id="@+id/text_view2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentBottom="true"
                android:textColor="@android:color/black"

                />
            <TextView
                android:id="@+id/text_view3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_alignParentBottom="true"
                android:textColor="@android:color/black"

                />


        </RelativeLayout>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="8dp"
        >
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_gravity="center"
            android:background="#303F9F"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"

        android:orientation="horizontal"
        >
        <ImageView
            android:id="@+id/image_view2"
            android:layout_height="match_parent"
            android:layout_width="130dp"

            android:src="@drawable/img_2"
            />
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"

            >
            <TextView
                android:id="@+id/text_view4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:textSize="20dp"
                android:textColor="@android:color/black"

                />
            <TextView
                android:id="@+id/text_view5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentBottom="true"
                android:textColor="@android:color/black"

                />
            <TextView
                android:id="@+id/text_view6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_alignParentBottom="true"
                android:textColor="@android:color/black"

                />
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#303F9F"/>

        </RelativeLayout>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="8dp"
        >
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_gravity="center"
            android:background="#303F9F"/>
    </LinearLayout>
</LinearLayout>

Detail_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    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=".DetailActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:orientation="horizontal"
        >
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"

            >
            <TextView
                android:id="@+id/text_view7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_alignParentTop="true"
                android:textSize="30dp"

                />
            <TextView
                android:id="@+id/text_view8"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentBottom="true"
                android:textSize="20dp"

                />
            <TextView
                android:id="@+id/text_view9"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_alignParentBottom="true"
                android:textSize="20dp"

                />


        </RelativeLayout>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="8dp"
        >
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_gravity="center"
            android:background="#303F9F"/>
    </LinearLayout>

    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        >

        <TextView
            android:id="@+id/text_view10"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textSize="20dp" />


    </LinearLayout>



</LinearLayout>

layout-land横屏布局

activity_list.xml  这个地方和竖屏的有一点点(亿点点)不同

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    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=".ListActivity">
    <LinearLayout
        android:orientation="vertical"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_weight="1.5"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="100dp"

            android:orientation="horizontal"
            >
            android:background="#303F9F"/>
            <ImageView
                android:id="@+id/image_view"
                android:layout_height="match_parent"
                android:layout_width="130dp"

                android:src="@drawable/img_1"
                />
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"

                >
                <TextView
                    android:id="@+id/text_view1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true"
                    android:textSize="20dp"
                    android:textColor="@android:color/black"

                    />
                <TextView
                    android:id="@+id/text_view2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentBottom="true"
                    android:textColor="@android:color/black"

                    />
                <TextView
                    android:id="@+id/text_view3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentBottom="true"
                    android:textColor="@android:color/black"

                    />


            </RelativeLayout>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="8dp"
            >
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_gravity="center"
                android:background="#303F9F"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="100dp"

            android:orientation="horizontal"
            >
            <ImageView
                android:id="@+id/image_view2"
                android:layout_height="match_parent"
                android:layout_width="130dp"

                android:src="@drawable/img_2"
                />
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"

                >
                <TextView
                    android:id="@+id/text_view4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true"
                    android:textSize="20dp"
                    android:textColor="@android:color/black"

                    />
                <TextView
                    android:id="@+id/text_view5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentBottom="true"
                    android:textColor="@android:color/black"

                    />
                <TextView
                    android:id="@+id/text_view6"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentBottom="true"
                    android:textColor="@android:color/black"

                    />
                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="#303F9F"/>

            </RelativeLayout>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="8dp"
            >
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_gravity="center"
                android:background="#303F9F"/>
        </LinearLayout>

    </LinearLayout>



    <LinearLayout
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#303F9F"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:layout_width="match_parent">
        <FrameLayout
            android:id="@+id/detail_fragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
           >
        </FrameLayout>


    </LinearLayout>

</LinearLayout>

实验总结:我是真的菜,是真的笨。

给我顶,加油,奥里给!

Guess you like

Origin www.cnblogs.com/westweishao/p/12078632.html