简体   繁体   English

在不使用片段的情况下以相同的布局更改屏幕

[英]change screens in the same layout without using fragments

I'm developing with Android Studio. 我正在使用Android Studio开发。 I created a screen that will have buttons on the side so it looks like a menu bar. 我创建了一个屏幕,该屏幕的侧面将具有按钮,因此它看起来像菜单栏。 i want to make the left (red side) layout changeable - means that if i click on one of the images on the right (green side) the left layout will change to a different layout and will open a different activity. 我想使左侧(红色)布局可更改-意味着,如果我单击右侧(绿色侧)上的图像之一,则左侧布局将更改为其他布局,并打开其他活动。 I already have the activities and their layouts that i want to show on the left side using the clickable images on the right side. 我已经有了要在右侧使用可点击图像在左侧显示的活动及其布局。 can i make it work without using fragments? 我可以不使用片段就可以工作吗?

the "menu" i have and how i want it to work 我拥有的“菜单”以及我希望它如何工作

the layout is: 布局是:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/ightwall" android:id="@+id/drawerlayout" android:weightSum="1" > <ScrollView android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="left" android:layout_weight=".7"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="My List" android:textAlignment="center" android:textSize="20dp" android:textStyle="bold" /> </LinearLayout> </ScrollView> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:orientation="vertical" android:background="#D5D4D4" android:layout_gravity="right" android:layout_weight=".3"> <ImageView android:id="@+id/mylist" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="3dp" android:src="@drawable/createlist_mylist" android:adjustViewBounds="true" android:maxHeight="100dp" android:maxWidth="100dp" android:layout_gravity="center" /> <ImageView android:id="@+id/freeadd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="3dp" android:src="@drawable/createlist_freetext" android:adjustViewBounds="true" android:maxHeight="100dp" android:maxWidth="100dp" android:layout_gravity="center" /> <ImageView android:id="@+id/categoryadd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="3dp" android:src="@drawable/createlist_categorysearch" android:adjustViewBounds="true" android:maxHeight="100dp" android:maxWidth="100dp" android:layout_gravity="center" /> <ImageView android:id="@+id/favproductadd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="3dp" android:src="@drawable/createlist_favproduct" android:adjustViewBounds="true" android:maxHeight="100dp" android:maxWidth="100dp" android:layout_gravity="center" /> </LinearLayout> </LinearLayout> 

You can use Replace Fragment 您可以使用“替换片段”

in OnClickListener 在OnClickListener中

 FragmentManager manager = getSupportFragmentManager();
    manager.beginTransaction()
            .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
            .replace(R.id.content_main, new TargetFragment())
            .commit();

And Change yourxml at 并在以下位置更改yourxml

 <ScrollView
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_gravity="left"
    android:layout_weight=".7">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="My List"

            android:textAlignment="center"
            android:textSize="20dp"
            android:textStyle="bold" />

    </LinearLayout>

</ScrollView>

To

 <ScrollView
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_gravity="left"
    android:layout_weight=".7">

  <FrameLayout
      android:id="@+id/content_main"
      android:layout_width="match_parent"
      android:layout_height="match_parent">

  </FrameLayout>

</ScrollView>

create class TargetFragment.class 创建类TargetFragment.class

 import android.support.v4.app.Fragment;
 public class TargetFragment extends Fragment {
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.target_fragment_xml, container, false);

 // Enter code here
 return root;
 }

The better and easy way is to use fragments. 更好,更简单的方法是使用片段。 However if you don't wan't to use fragments, then you can have all the views in your activity's xml in some separate relative or linear layout, whichever you prefer and play with hide-show views with each click. 但是,如果您不想使用片段,则可以将所有视图以某种独立的相对或线性布局放置在活动xml中,以您喜欢的任何一种为准,并在每次单击时都使用隐藏视图。

The easy way is to use fragments. 最简单的方法是使用片段。 Fragments are designed to be reusable UI layouts. 片段被设计为可重用的UI布局。 But if you don't want to use fragments, you can create CustomView and use addView , removeView like example: 但是,如果您不想使用片段,则可以创建CustomView并使用addViewremoveView ,例如:

LinearLayout layout=new LinearLayout(context);
layout.addView(new  Button(context));
layout.addView(new ImageButton(context));

TextView view = new TextView(context);
view.setText("Hello World");
layout.addView(view); 

layout.removeView(view);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM