【问题标题】:How can i change the UI when in portrait and landscape在纵向和横向时如何更改 UI
【发布时间】:2016-03-02 18:16:29
【问题描述】:

我知道这里已经存在关于使用方向和横向布局的问题,但是我想做的有点不同。我的目标是在旋转到横向时完全更改 UI,例如当纵向显示常规信息但旋转时显示表格时。

这是否会涉及两个独立的活动并使用 if 语句根据方向启动活动?我尝试使用简单的横向布局,但是当我更改方向时,它会在纵向布局中使用的所有 findviewbyids 的 nullpointerexception 上关闭。

为manusunny添加代码

    public class PortraitFragment extends FragmentActivity {
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

            View view =  inflater.inflate(R.layout.content_main, container, false);
            ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
            TextView lessonString = (TextView) view.findViewById(R.id.primaryText);
            TextView subTextString = (TextView) view.findViewById(R.id.secondaryText);
            return view;
        }
    }


public class LandscapeFragment extends FragmentActivity {

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.content_portrait, container, false);
        ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
        TextView lessonString = (TextView) view.findViewById(R.id.primaryText);
        TextView subTextString = (TextView) view.findViewById(R.id.secondaryText);
        return view;

    }
}

MainActivty 整体

package com.example.harris.doitrightyoustupidcretin;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;

import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
import android.support.v4.app.FragmentActivity;
import com.example.harris.doitrightyoustupidcretin.R;

import org.joda.time.LocalTime;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MyLog";


    String historyInfo[] = new String[2];
    String computingInfo[] = new String[2];
    String reInfo[] = new String[2];
    String biologyInfo[] = new String[2];
    String physicsInfo[] = new String[2];
    String chemistryInfo[] = new String[2];
    String frenchInfo[] = new String[2];
    String englishInfo[] = new String[2];
    String mathsInfo[] = new String[2];
    String gamesInfo[] = new String[2];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        historyInfo[0] = "H1";
        historyInfo[1] = "Miss Bradbury";

        mathsInfo[0] = "MA1";
        mathsInfo[1] = "Miss Lawrence";

        biologyInfo[0] = "BL1";
        biologyInfo[1] = "Mr Gibson";

        chemistryInfo[0] = "CL2";
        chemistryInfo[1] = "Dr Bowers";

        frenchInfo[0] = "LA8";
        frenchInfo[1] = "Miss Bailey";

        reInfo[0] = "RE2";
        reInfo[1] = "Mr Dimmock";

        englishInfo[0] = "ER1";
        englishInfo[1] = "Miss Richards";

        gamesInfo[0] = "Sports Hall";
        gamesInfo[1] = "Mr Pugh";

        computingInfo[0] = "IT1";
        computingInfo[1] = "Mr Green";

        physicsInfo[0] = "PL2";
        physicsInfo[1] = "Mr Evans";


        String nextLessonString = "default";



        nextlesson();

        setLessonText();



        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                nextlesson();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    public void setLessonText(){
        ImageView imageView = (ImageView) findViewById(R.id.imageView);
        TextView lessonString = (TextView) findViewById(R.id.primaryText);
        TextView subTextString = (TextView) findViewById(R.id.secondaryText);

        lessonString.setText(nextlesson());

        if (nextlesson()== "History"){
            imageView.setImageResource(R.drawable.history);
            subTextString.setText(historyInfo[1] + " - "+historyInfo[0]);
        }
        if (nextlesson()== "Maths"){
            imageView.setImageResource(R.drawable.maths);
            subTextString.setText(mathsInfo[1] + " - " + mathsInfo[0]);
        }
        if (nextlesson()== "English"){
            imageView.setImageResource(R.drawable.english);
            subTextString.setText(englishInfo[1] + " - " + englishInfo[0]);
        }
        if (nextlesson()== "French"){
            imageView.setImageResource(R.drawable.french);
            subTextString.setText(frenchInfo[1] + " - " + frenchInfo[0]);
        }
        if (nextlesson()== "Computing"){
            imageView.setImageResource(R.drawable.computing);
            subTextString.setText(computingInfo[1] + " - " + computingInfo[0]);
        }
        if (nextlesson()== "Biology"){
            imageView.setImageResource(R.drawable.biology);
            subTextString.setText(biologyInfo[1] + " - " + biologyInfo[0]);
        }
        if (nextlesson()== "Chemistry"){
            imageView.setImageResource(R.drawable.chemistry);
            subTextString.setText(chemistryInfo[1] + " - " + chemistryInfo[0]);
        }
        if (nextlesson()== "Physics"){
            imageView.setImageResource(R.drawable.physics);
            subTextString.setText(physicsInfo[1] + " - " + physicsInfo[0]);
        }
        if (nextlesson()== "RE"){
            imageView.setImageResource(R.drawable.re);
            subTextString.setText(reInfo[1] + " - " + reInfo[0]);
        }
    }


    public String nextlesson() {
        Calendar c = Calendar.getInstance();
        int weekOfMonth = c.get(Calendar.WEEK_OF_MONTH);
        LocalTime currentTime = LocalTime.now();
        int nextLessonNumber = 0;
        String nextLessonString;
        int currentLesson = 0;

        LocalTime lesson1Start = new LocalTime("09:15");
        LocalTime lesson1End = new LocalTime("10:15");
        LocalTime lesson2Start = new LocalTime("10:15");
        LocalTime lesson3Start = new LocalTime("11:40");
        LocalTime lesson4start = new LocalTime("13:45");
        LocalTime lesson5Start = new LocalTime("14:45");
        LocalTime endOfDay = new LocalTime ("15:50");

        int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
        int dateDays = c.get(Calendar.DAY_OF_MONTH);
        int dateMonth = c.get(Calendar.MONTH);

        if (currentTime.isBefore(lesson1Start)) {
            //p1 next
            nextLessonNumber = 1;
            Log.d(TAG, "It worked");
            currentLesson = 0;
        } else if (currentTime.isAfter(lesson1Start) && currentTime.isBefore(lesson1End)) {
            //p2 next
            nextLessonNumber = 2;
            Log.d(TAG, "It worked");
            currentLesson = 1;
        } else if (currentTime.isAfter(lesson2Start) && currentTime.isBefore(lesson3Start)) {
            //p3 next
            nextLessonNumber = 3;
            Log.d(TAG, "It worked");
            currentLesson = 2;
        } else if (currentTime.isAfter(lesson3Start) && currentTime.isBefore(lesson4start)) {
            //p4 next
            nextLessonNumber = 4;
            Log.d(TAG, "It worked");
            currentLesson = 3;
        } else if (currentTime.isAfter(lesson4start) && currentTime.isBefore(lesson5Start)) {
            //p5 next
            nextLessonNumber = 5;
            Log.d(TAG, "It worked");
            currentLesson = 4;
        } else if (currentTime.isAfter(lesson5Start) && currentTime.isAfter(endOfDay)) {
            currentLesson = 5;
        } else {
            nextLessonNumber = 0;
            Log.d(TAG, "It didn't worked");
        }





        if ((weekOfMonth == 1) || (weekOfMonth == 3)) {
            Log.d(TAG, "It made it this far");

            switch (dayOfWeek) {
                case 1: //Sunday DO NOTHING

                    return "Sunday";
                case 2: //Monday WEEK B
                    if (nextLessonNumber == 1) {
                        return "Maths";

                    } else if (nextLessonNumber == 2) {
                        return "Physics";
                    } else if (nextLessonNumber == 3) {
                        return "History";
                    } else if (nextLessonNumber == 4) {
                        return "Chemistry";
                    } else if (nextLessonNumber == 5) {
                        return "French";
                    }else {

                    }
                    break;

                case 3: //Tuesday WEEK B
                    Log.d(TAG, "Yo it worked yo");
                    if (nextLessonNumber == 1) {
                        return "RE";

                    } else if (nextLessonNumber == 2) {
                        return "English";
                    } else if (nextLessonNumber == 3) {
                        return "Maths";
                    } else if (nextLessonNumber == 4) {
                        return "Computing";
                    } else if (nextLessonNumber == 5) {
                        return "Biology";
                    } else {

                    }
                    break;

                case 4: //Wednesday WEEK B
                    if (currentLesson == 1) {
                        return "English";

                    } else if (nextLessonNumber == 2) {
                        return "Computing";
                    } else if (nextLessonNumber == 3) {
                        return "Chemistry";
                    } else if (nextLessonNumber == 4) {
                        return "French";
                    } else if (nextLessonNumber == 5) {
                        return "History";
                    }else {

                    }
                    break;

                case 5: //Thursday WEEK B
                    Log.d(TAG, "ThursWeekB");
                    if (nextLessonNumber == 1) {
                        return "French";

                    } else if (nextLessonNumber == 2) {
                        return "History";
                    } else if (nextLessonNumber == 3) {
                        return "Physics";
                    } else if (nextLessonNumber == 4) {
                        return "Games";
                    } else if (nextLessonNumber == 5) {
                        return "Games";
                    }else {

                    }
                    break;

                case 6: //Friday WEEK B
                    if (nextLessonNumber == 1) {
                        return "RE";

                    } else if (nextLessonNumber == 2) {
                        return "Biology";
                    } else if (nextLessonNumber == 3) {
                        return "Computing";
                    } else if (nextLessonNumber == 4) {
                        return "English";
                    } else if (nextLessonNumber == 5) {
                        return "Maths";
                    }else {

                    }
                    break;
                case 7: //Saturday DO NOTHING
                        return "Saturday";

            }

        } else {

            switch (dayOfWeek) {
                case 1: //Sunday DO NOTHING

                    return "Sunday";
                case 2: //Monday WEEK A
                    if (nextLessonNumber == 1) {
                        return "Maths";

                    } else if (nextLessonNumber == 2) {
                        return "English";

                    } else if (nextLessonNumber == 3) {
                        return "Computing";

                    } else if (nextLessonNumber == 4) {
                        return "RE";
                    } else if (nextLessonNumber == 5) {
                        return "Physics";
                    }else {

                    }
                    break;

                case 3: //Tuesday WEEK A
                    Log.d(TAG, "It worked yo pNOTHING");
                    if (nextLessonNumber == 1) {
                        return "Maths";

                    } else if (nextLessonNumber == 2) {
                        return "French";
                    } else if (nextLessonNumber == 3) {
                        return "History";
                    } else if (nextLessonNumber == 4) {
                        return "English";
                    } else if (nextLessonNumber == 5) {
                        return "Chemistry";
                    }else {

                    }
                    break;

                case 4: //Wednesday WEEK A
                    if (nextLessonNumber == 1) {
                        return "English";
                    } else if (nextLessonNumber == 2) {
                        return "Computing";
                    } else if (nextLessonNumber == 3) {
                        return "Biology";
                    } else if (nextLessonNumber == 4) {
                        return "History";
                    } else if (nextLessonNumber == 5) {
                        return "French";
                    }else {

                    }
                    break;

                case 5: //Thursday WEEK A
                    Log.d(TAG, "It worked yo thurs");
                    if (nextLessonNumber == 1) {
                        return "Chemistry";

                    } else if (nextLessonNumber == 2) {
                        return "French";
                    } else if (nextLessonNumber == 3) {
                        return "Maths";
                    } else if (nextLessonNumber == 4) {
                        return "Games";
                    } else if (nextLessonNumber == 5) {
                        return "Games";
                    }else {

                    }
                    break;


                case 6: //Friday WEEK A
                    if (nextLessonNumber == 1) {
                        return "History";

                    } else if (nextLessonNumber == 2) {
                        return "Physics";
                    } else if (nextLessonNumber == 3) {
                        return "Computing";
                    } else if (nextLessonNumber == 4) {
                        return "Maths";
                    } else if (nextLessonNumber == 5) {
                        return "Biology";
                    }else {

                    }
                    break;
                case 7: //Saturday DO NOTHING
                    return "Saturday";

            }
        }

return null;
    }
}

【问题讨论】:

    标签: java android xml android-studio


    【解决方案1】:

    使用一个活动和两个片段。看看这个例子,

    MainActivity.java

    public class MainActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
        }
    }
    

    res/layout/activity_main.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"
        tools:context="com.manusunny.myapplication.MainActivity"
        android:orientation="vertical">
    
        <fragment android:name="com.manusunny.myapplication.PortraitFragment"
            android:id="@+id/portrait_fragment"
            android:layout_width="match_parent"
            android:layout_height="30dp" />
    </LinearLayout>
    

    res/layout-land/activity_main.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"
        tools:context="com.manusunny.myapplication.MainActivity"
        android:orientation="vertical">
    
        <fragment android:name="com.manusunny.myapplication.LandscapeFragment"
            android:id="@+id/landscape_fragment"
            android:layout_width="match_parent"
            android:layout_height="30dp" />
    </LinearLayout>
    

    PortraitFragment.java

    public class PortraitFragment extends Fragment {
        public PortraitFragment() {
            // Required empty public constructor
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_portrait, container, false);
            // do something
            return view
        }
    }
    

    res/layout/fragment_portrait.xml

    <FrameLayout 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"
        tools:context="com.manusunny.myapplication.PortraitFragment">
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Portrait" />
    </FrameLayout>
    

    LandscapeFragment.java

    public class LandscapeFragment extends Fragment {
        public LandscapeFragment() {
            // Required empty public constructor
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_landscape, container, false);
            // do something
            return view
        }
    }
    

    res/layout/fragment_landscape.xml

    <FrameLayout 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"
        tools:context="com.manusunny.myapplication.LandscapeFragment">
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Landscape" />
    </FrameLayout>
    

    这将在纵向屏幕中显示'Portrait',在横向屏幕中显示'Landscape'

    希望对你有帮助。

    【讨论】:

    • 我不确定我是否完全理解。我是否在两个 xml 文件中都包含一个片段?片段标签中有什么内容?
    • 请阅读修改后的答案。并阅读this ..
    • 感谢您的快速回复。我收到错误消息 - 膨胀类片段时出错。我的两个片段看起来都像这样-
    • 查找相关示例
    • 当类设置为扩展片段时,我收到内存不足错误,当我将其更改为 AppCompatActivity 时,因为其他 SO 线程建议它说试图实例化一个不是片段的类。跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多