gpt4 book ai didi

android - 与线性布局一起使用时不显示 FrameLayouts

转载 作者:行者123 更新时间:2023-11-29 17:54:34 27 4
gpt4 key购买 nike

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

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

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

非常感谢。

这里是 Activity 类:

[主 Activity .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>

最佳答案

试试这个..

您已将 LinearLayout height 指定为 match_parent,因此它将匹配整个父级,这就是 FrameLayout 未显示的原因。将 LinearLayout height 设置为 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>

关于android - 与线性布局一起使用时不显示 FrameLayouts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20366949/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com