<少女> 2022-11-26 22:50 采纳率: 90.9%
浏览 13
已结题

想要写一个fragment,但是为什么写不出来啊?请问这里为什么显示不出来啊?详解必采纳


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:id="@+id/list"
    android:name="com.example.studytest06.ListFragment1"
    android:layout_weight="1"/>
    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:name="com.example.studytest06.DetailFragment"
        android:id="@+id/detail"
        android:layout_weight="1"/>
</LinearLayout>
package com.example.studytest06;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class DetailFragment extends AppCompatActivity {

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

package com.example.studytest06;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ListFragment1 extends AppCompatActivity {
//    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//        View view = inflater.inflate(R.layout.fragment_list, container, false);
//        return view;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_fragment1);
    }
    }


package com.example.studytest06;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

public class MainActivity extends Activity {

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==0x11&&requestCode==0x11){
            Bundle bundle=data.getExtras();
            int imageId=bundle.getInt("imageId");
            ImageView imageView=findViewById(R.id.touxiang);
            imageView.setImageResource(imageId);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main1);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DetailFragment">
   <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Detail Fragment"
    android:textSize="30sp"/>
</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ListFragment1">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="ListView Fragment"
    android:textSize="30sp"/>
</LinearLayout>

请问这里为什么显示不出来啊?详解必采纳。我想要的效果图:

img

  • 写回答

1条回答 默认 最新

  • 天兰之珠 2022-11-28 11:47
    关注

    1:首先你要写一个fragment,你想在一个MainActivity 的布局里显示由ListFragment1和DetailFragment两个模块组成的界面,那首先这两个得是Fragment类,继承Fragment或者他的子类
    2:其次在MainActivity 的布局里得用<fragment来加载这个些Fragment
    具体代码如下,记得运行就可看到效果了

    public class DetailFragment extends Fragment {
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            //加载布局
            return inflater.inflate(R.layout.activity_detail_fragment, container, false);
        }
    }
    
    <LinearLayout 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=".DetailFragment">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Detail Fragment"
            android:textSize="30sp" />
    </LinearLayout>
    
    public class ListFragment1  extends Fragment {
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            //加载布局
            return inflater.inflate(R.layout.activity_list_fragment1, container, false);
        }
    }
    
    
    <LinearLayout 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=".ListFragment1">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ListView Fragment"
            android:textSize="30sp"/>
    </LinearLayout>
    
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    }
    
    
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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"
        android:orientation="horizontal">
    
        <fragment
            android:id="@+id/list"
            android:name="com.snxun.gzgynm.myapplication.ListFragment1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            tools:ignore="Instantiatable" />
    
        <fragment
            android:id="@+id/detail"
            android:name="com.snxun.gzgynm.myapplication.DetailFragment"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    
    </LinearLayout>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 12月11日
  • 已采纳回答 12月3日
  • 创建了问题 11月26日

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改