读书人

为什么小弟我的Fragment UI 显示不出来

发布时间: 2013-08-01 15:23:18 作者: rapoo

为什么我的Fragment UI 显示不出来
Fragement类

package com.example.testfragement;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MyFragement extends Fragment{

@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {//创建Fragement所展现的视图
return inflater.inflate(R.layout.example_fragment, container,false);
}

@Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
}

}


R.layout.example_fragment
布局

<?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" >


<Button android:id="@+id/btn" android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="点击"/>
<TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="hello" android:textSize="100.0dip"/>
</LinearLayout>

主activity界面布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="@+id/h1"
android:layout_height="match_parent" >



<fragment android:name="com.example.testfragement.MyFragement"
android:id="@+id/viewer"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />




</RelativeLayout>


住activity
package com.example.testfragement;

import android.os.Bundle;
import android.app.Activity;

import android.support.v4.app.FragmentActivity;
import android.view.Menu;


public class MainActivity extends FragmentActivity {

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

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}


}



[解决办法]
<fragment android:name="com.example.testfragement.MyFragement" android:id="@+id/viewer" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" />

你这里android:layout_width="0dp"当然显示不出来了,改成android:layout_width="match_parent"

另外这个 android:layout_weight="1" 在这没什么作用,你就一个fragment控件

读书人网 >Android

热点推荐