Android serial 14 - write fragments for different resolutions

1, Edit layout

1. Write two layouts respectively: one for large screen resolution such as tablet, and the other for small screen resolution such as mobile phone. Write the small one first. Address: layout/activity_main.xml

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent">

   

    <fragment

        android:id="@+id/news_title_fragment"

        android:name="com.example.fragmentbestpractice.NewsTitleFragment"

        android:layout_width="match_parent"

        android:layout_height="match_parent" />

   

   

</LinearLayout>

And then layout and write a high-resolution layout in parallel

Address: layout-sw600dp/activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent" >

   

    <fragment

        android:id="@+id/news_title_fragment"

        android:name="com.example.fragmentbestpractice.NewsTitleFragment"

        android:layout_width="0dp"

        android:layout_height="match_parent"

        android:layout_weight="1" />

   

    <FrameLayout

        android:id="@+id/news_content_layout"

        android:layout_width="0dp"

        android:layout_height="match_parent"

        android:layout_weight="3" >

       

        <fragment

            android:id="@+id/news_content_fragment"

            android:name="com.example.fragmentbestpractice.NewsContentFragment"

            android:layout_width="match_parent"

            android:layout_height="match_parent" />

    </FrameLayout></LinearLayout>

Two interfaces are written.

2, Modify main activity

package com.example.fragmentbestpractice;

​

import android.app.Activity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.Window;

​

public class MainActivity extends Activity {

​

  @Override

  protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.activity_main);

  }

​

  @Override

  public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.

    getMenuInflater().inflate(R.menu.main, menu);

    return true;

  }

​

  @Override

  public boolean onOptionsItemSelected(MenuItem item) {

    // Handle action bar item clicks here. The action bar will

    // automatically handle clicks on the Home/Up button, so long

    // as you specify a parent activity in AndroidManifest.xml.

    int id = item.getItemId();

    if (id == R.id.action_settings) {

      return true;

    }

    return super.onOptionsItemSelected(item);

  }

}

 

Run our APP like this

 

android has been running for many days. I forgot the previous things. The future android series will focus on reviewing what I learned before. ​

3, Source code:

1. Project address

https://github.com/ruigege66/Android/tree/master/FragmentBestPractise

2.CSDN: https://blog.csdn.net/weixin_44630050

3. Blog Park: https://www.cnblogs.com/ruigege0000/

4. welcome to WeChat official account: Fourier transform, official account number, only for learning communication, background reply, "gift package", get big data learning materials.

 

Tags: Android xml Fragment Mobile

Posted on Fri, 29 May 2020 10:43:11 -0400 by Vasudhevan