Resize layout to get into other layout

2019-06-03 00:14发布

问题:

I created a menuView.xml layout to be in all of the layouts of my activity. This layout has one column on each border and a title bar like this:

ComposeView http://img845.imageshack.us/img845/2121/d6zp.png

I insert this layout in the other layouts this way:

<!-- Show menu -->
<com.example.MenuView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

But if one of the layouts has full screen view, part of this view gets covered by the MenuView, so...

How could I tell to this view to adapt its size to the blank space inside the MenuView to not get covered by it?

UPDATE -- full XML included

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" 
    android:background="@drawable/degradado">


    <!-- Show menu -->
    <com.example.MenuView
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


    <RelativeLayout 
        android:id="@+id/Left_layout"
        android:layout_width="fill_parent"        
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true" >

    //Here go buttons, views, etc...

    </RelativeLayout>

    <RelativeLayout 
        android:id="@+id/Right_layout"
        android:layout_width="fill_parent"        
        android:layout_height="fill_parent"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true" > 

    //Here go buttons, views, etc...

</RelativeLayout>

What happens here is that these 2 Relative layouts get covered by the MenuView (The darkest gre borders and the top black bar), and the ideal way would be that these 2 layouts get fitted to the blank space (the clearest gray).

I can solve this setting margin sizes to the Relative layouts to fit inside of it, but i know this is not the best way to do it, so I don't know if there is another way.

回答1:

I think the best way to solve your issue is with inheritance. If you define an Activity that can be used as a template for all your fleshed out Activitys to add their content to. I don't know what you custom menu is 'made of' but as a simple example:

Create a basic Activity with code:

  public class ActivityWithMenu extends Activity 
  {
      @Override
      protected void onCreate(Bundle savedInstanceState) 
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_with_menu_layout);
      }
  }

and xml:

  <RelativeLayout 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=".ActivityWithMenu" >
<TextView
    android:layout_width="match_parent"
    android:layout_height="20dip"  
    android:background="#ff000000"  
    android:textColor="#ffffffff"    
    android:text="Main Menu Title Bar"
    android:id="@+id/mainmenutitle" />
<LinearLayout 
    android:layout_width="20dip"
    android:layout_height="match_parent"
    android:layout_below="@+id/mainmenutitle"
    android:layout_alignParentLeft="true"
    android:background="#ff999999"
    android:orientation="vertical"
    android:id="@+id/lefthandmenu" />
<LinearLayout 
    android:layout_width="20dip"
    android:layout_height="match_parent"
    android:layout_below="@+id/mainmenutitle"
    android:layout_alignParentRight="true"
    android:background="#ff999999"
    android:orientation="vertical"
    android:id="@+id/righthandmenu" />
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_toLeftOf="@+id/righthandmenu"
    android:layout_toRightOf="@+id/lefthandmenu"
    android:layout_below="@+id/mainmenutitle"
    android:orientation="vertical"
    android:id="@+id/activitycontent"
    />
  </RelativeLayout>

Then create your xml for a specific Activity, in this case a simple 'Hello World' layout:

  <?xml version="1.0" encoding="utf-8"?>
  <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" >
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ff00ff00"
        android:text="Hello World!"
        />
</LinearLayout>
  </ScrollView>

But now when you write the code for this Activity, extend 'ActivityWithMenu' instead of the Activity class direct and inflate this xml layout as follows:

  public class Activity1 extends ActivityWithMenu
  {
      @Override
      protected void onCreate(Bundle savedInstanceState) 
      {
          requestWindowFeature(Window.FEATURE_NO_TITLE);
          getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,    
                  WindowManager.LayoutParams.FLAG_FULLSCREEN);    

          super.onCreate(savedInstanceState);

          LinearLayout ll = (LinearLayout)findViewById(R.id.activitycontent);

          ScrollView sv = (ScrollView)this.getLayoutInflater().inflate(R.layout.activity1_layout, ll, false);

          ll.addView(sv);
      }
  }

I have added the code for making the Activity fullscreen here instead of in the parent ActivityWithMenu class assuming that you wouldn't want them all displayed that way but you could move it into the parent class if appropriate.

Hope this helps.