Android 仅在特定方向上显示视图组的步骤

Android 仅在特定方向上显示视图组的步骤,android,android-layout,android-fragments,android-ui,Android,Android Layout,Android Fragments,Android Ui,我要建立一个新闻应用程序,其中有一个新闻列表。点击时,应显示详细新闻。 1.如果手机是纵向的,则详细信息将显示在另一个活动中 2.如果手机处于横向模式,则在同一活动的详细信息版面片段中,详细信息将显示在新闻列表旁边 有一段“新闻列表”和“详细信息” 我无法做到的是去掉细节片段所占据的空间——如果我在横向模式下——在纵向模式下 这是因为我提到了两个空视图组作为活动布局文件中片段的容器 我不想为每个方向做单独的布局 如果视图组为空,是否有方法隐藏或精简视图组 相关代码: 主要活动xml: 提前感谢

我要建立一个新闻应用程序,其中有一个新闻列表。点击时,应显示详细新闻。 1.如果手机是纵向的,则详细信息将显示在另一个活动中 2.如果手机处于横向模式,则在同一活动的详细信息版面片段中,详细信息将显示在新闻列表旁边

有一段“新闻列表”和“详细信息”

我无法做到的是去掉细节片段所占据的空间——如果我在横向模式下——在纵向模式下

这是因为我提到了两个空视图组作为活动布局文件中片段的容器

我不想为每个方向做单独的布局

如果视图组为空,是否有方法隐藏或精简视图组

相关代码:

主要活动xml:


提前感谢您

在您的情况下,您需要设置包含要查看的详细信息片段的布局的可见性。View.GONE意味着布局不可见且不占用空间。View.INVISIBLE表示视图不可见,但空间仍被占用

LinearLayout detailsContainer = (LinearLayout) findViewById(R.id.details_container);
int orientation = getResources().getConfiguration.orientation;
if (orientation == 1) // means orientations is portrait
    detailsContainer.setVisibility(View.GONE);

我用details\u view.setVisibilityView.GONE解决了我的问题;感谢所有关心此事的人
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //get orientation first
        if(this.getResources().getConfiguration().orientation==1) { //portrait

            NewsList_fragment newsFrag = new NewsList_fragment(); //init any fragments that may be used

            FragmentManager fManager = getFragmentManager(); //prepare fragment manager
            FragmentTransaction fTransaction = fManager.beginTransaction();

            // Add fragments using fTransaction and then commit
            fTransaction.add(R.id.news_list_container, newsFrag);
            fTransaction.commit();
            }

        else if (this.getResources().getConfiguration().orientation==2) { //landscape

            NewsList_fragment newsFrag = new NewsList_fragment(); //init any fragments that may be used
            Details_fragment detailsFrag = new Details_fragment();


            FragmentManager fManager = getFragmentManager(); //prepare fragment manager
            FragmentTransaction fTransaction = fManager.beginTransaction();

            // Add fragments using fTransaction and then commit
            fTransaction.add(R.id.news_list_container, newsFrag);
            fTransaction.add(R.id.details_container, detailsFrag);

            fTransaction.commit();
            }
LinearLayout detailsContainer = (LinearLayout) findViewById(R.id.details_container);
int orientation = getResources().getConfiguration.orientation;
if (orientation == 1) // means orientations is portrait
    detailsContainer.setVisibility(View.GONE);