与“线性布局”一起使用时,未显示FrameLayouts

时间:2013-12-04 04:37:06

标签: android android-layout android-fragments

我想测试使用片段管理器动态替换片段。在主布局中,在父线框布局下,我有3个布局,一个子线性布局和两个子框架布局。我们的想法是在第一个子线性布局中使用一个按钮来交换第三帧布局中的片段。

我看到的问题:加载应用程序时,屏幕上只显示子线性布局;片段及其框架布局容器不可见。有人能说出我在这里做错了吗? LogCat中没有特别的错误......

我尝试调整子布局的权重参数来解决此问题,但没有太大的成功。

提前多多感谢。

以下是活动类:

[MainActivity.java]

package com.jiao.android.todolist;

import java.util.ArrayList;


import android.app.Activity;
import android.os.Bundle;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class MainActivity extends Activity implements NewItemFragment.OnNewItemAddedListener {

private ArrayAdapter<String> aa;
private ArrayList<String> todoItems;
private ArrayAdapter<String> aa2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Get reference to the Fragments
    FragmentManager fm = getFragmentManager();
    ToDoListFragment todoListFragment = (ToDoListFragment)fm.findFragmentById(R.id.list_container);

    //The codes below shows how to load fragments dynamically through Fragment Manager transaction
    //into the predefined frame container in the layout xml
    if(todoListFragment == null)


    {
        todoListFragment = new ToDoListFragment();

        FragmentTransaction ft = fm.beginTransaction();
        ft.add(R.id.item_container, new NewItemFragment());
        ft.add(R.id.list_container, todoListFragment);
        ft.commit();
    }



    //Create the ArrayList of to do items
    todoItems = new ArrayList<String>();

    //Create the ArrayAdapter to bind the array to the List View


    aa = new ArrayAdapter<String>(this,R.layout.todolist_item,todoItems);

    //Bind the Array Adapter to the List View
    todoListFragment.setListAdapter(aa);








}

@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;
}

public void onNewItemAdded(String newItem){
    todoItems.add(newItem);
    aa.notifyDataSetChanged();
}


public void onSwapButtonClicked (View view)
{
    //Get reference to the Fragments
            FragmentManager fm2 = getFragmentManager();
            ToDoListPlainFragment todoListFragment2 = new ToDoListPlainFragment();

            //The codes below shows how to load fragments dynamically through Fragment Manager transaction



                FragmentTransaction ft2 = fm2.beginTransaction();
                String myNewFragmentTag = null;
                ft2.replace(R.id.list_container, todoListFragment2,myNewFragmentTag);

                ft2.commit();






            //Create the ArrayAdapter to bind the array to the List View


            aa2 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,todoItems);

            //Bind the Array Adapter to the List View
            todoListFragment2.setListAdapter(aa2);



}

}

[NewItemFragment.java]

package com.jiao.android.todolist;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

public class NewItemFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    View view = inflater.inflate(R.layout.new_item_fragment,container,false);

    final EditText myEditText = (EditText)view.findViewById(R.id.myEditText);

    //this is how an editText can respond to DPAD_CENTER
            //or the enter key
            myEditText.setOnKeyListener(new View.OnKeyListener() {

                @Override
                public boolean onKey(View v, int keyCode, KeyEvent event) {
                    // TODO Auto-generated method stub

                    if(event.getAction()==KeyEvent.ACTION_DOWN)
                        if((keyCode == KeyEvent.KEYCODE_DPAD_CENTER)||
                        (keyCode == KeyEvent.KEYCODE_ENTER))
                        {
                           String newItem = myEditText.getText().toString();
                           onNewItemAddedListener.onNewItemAdded(newItem);
                           myEditText.setText("");


                           return true;

                        }

                    return false;
                }
            });

            return view;
}

public interface OnNewItemAddedListener {
    public void onNewItemAdded(String newItem);
}

private OnNewItemAddedListener onNewItemAddedListener;

@Override
public void onAttach(Activity activity)
{
    super.onAttach(activity);

    try{
        onNewItemAddedListener = (OnNewItemAddedListener)activity;

    }catch (ClassCastException e)
    {
        throw new ClassCastException(activity.toString()+" must implement OnNewItemAddedListener");
    }
}

}

[MyTextView.java]

package com.jiao.android.todolist;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.widget.TextView;

public class MyTextView extends TextView{




public MyTextView(Context context,AttributeSet attrs, int defStyle)
{
    super(context,attrs, defStyle);
    // do not forgot to call the ini() method, otherwise, allt he paint objects
    // won't be initialized
    init();
}

public MyTextView (Context context)
{
    super(context);
    // do not forgot to call the ini() method, otherwise, allt he paint objects
    // won't be initialized
    init();
}

public MyTextView(Context context, AttributeSet attrs)
{
    super(context, attrs);
    // do not forgot to call the ini() method, otherwise, allt he paint objects
    // won't be initialized
    init();
}

private Paint marginPaint;
private Paint linePaint;
private int paperColor;
private float margin;

private void init(){
    //Get a referencve to our resource table
    Resources myResources = getResources();

    //Create the paint brushes we will use in the onDraw method.
    marginPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    marginPaint.setColor(myResources.getColor(R.color.notepad_margin));
    linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    linePaint.setColor(myResources.getColor(R.color.notepad_lines));
    System.out.println("line paint is"+linePaint.getColor());

    //Get the paper background color and the margin width.
    paperColor = myResources.getColor(R.color.notepad_paper);
    margin = myResources.getDimension(R.dimen.notepad_margin);
}

@Override
public void onDraw(Canvas canvas){


    //Color as paper
    canvas.drawColor(paperColor);

    //Draw ruled lines
    //drawLine(float startX, float startY, float stopX, float stopY, Paint paint) 
    canvas.drawLine(0, 0,0,  getMeasuredHeight(), linePaint);
    canvas.drawLine(0, getMeasuredHeight(), getMeasuredWidth(), getMeasuredHeight(), linePaint);

    //Draw Margin
    canvas.drawLine(margin,0,margin,getMeasuredHeight(), marginPaint);

    //Move the text across from the margin
    canvas.save();
    //The translate() method remaps the (0,0) position on the canvas
    //or you can think you move the canvas
    //Translate - Basically do what it says. Just translate the canvas using x,y. 
    //If you want to draw two objects and the one is just translation of the other e.g x2 = x1 + 50 for each point . 
    //You don't have to make all your calculations again for the second object but you can just translate the canvas 
    //and draw again the same object. 
    canvas.translate(margin,0);
    super.onDraw(canvas);

    //This call balances a previous call to save(), 
    //and is used to remove all modifications to the matrix/clip state since the last save call.
    //canvas.save and canvas.restore undo things like rotation and translation. They don't undo drawing on the canvas.
    canvas.restore();

}

@Override
public boolean onKeyDown(int keyCode, KeyEvent keyEvent){
    return super.onKeyDown(keyCode, keyEvent);
}


}

[ToDoListFragment.java]

package com.jiao.android.todolist;

import android.app.ListFragment;

 public class ToDoListFragment extends ListFragment {

 }

[ToDoListPlainFragment.java]

 package com.jiao.android.todolist;

 import android.app.ListFragment;

 public class ToDoListPlainFragment extends ListFragment {

 }

下面是布局xml文件:

[activity_main.xml中]

<?xml version="1.0" encoding="utf-8"?>

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

android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"

 >
<LinearLayout 

android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
        android:id="@+id/swapButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/swap_button_text" 
        android:onClick="onSwapButtonClicked" 
        android:layout_weight="2"
        />

</LinearLayout>
<FrameLayout 
    android:id="@+id/item_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1" />

<FrameLayout 
    android:id="@+id/list_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1" />









</LinearLayout>

[new_item_fragment.xml]

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/addItemHint"
android:contentDescription="@string/addItemContentDescription" />

[todolist_item.xml]

<?xml version="1.0" encoding="utf-8"?>
<com.jiao.android.todolist.MyTextView
xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:scrollbars="vertical"
android:textColor="@color/notepad_text"
android:fadingEdge="vertical" 
/>

这是[AndroidManifest.xml]

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jiao.android.todolist"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="11" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.jiao.android.todolist.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

1 个答案:

答案 0 :(得分:3)

试试这个..

您已将LinearLayout 身高设为match_parent,因此它将匹配整个父级,这就是FrameLayout未显示的原因。将LinearLayout 身高作为wrap_content并尝试一下。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"  // change is here
        android:orientation="horizontal" >

        <Button
            android:id="@+id/swapButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:onClick="onSwapButtonClicked"
            android:text="swap_button_text" />
    </LinearLayout>

    <FrameLayout
        android:id="@+id/item_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <FrameLayout
        android:id="@+id/list_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" />

</LinearLayout>