fragment跳转到fragment 位置不对

day_moon 2016-01-05 04:13:17
点击第一个图片按钮后,运行的结果是:

我想要的结果:


MyFragment1类代码如下:
public class MyFragment1 extends Fragment implements View.OnClickListener{
ImageButton ib_update;
public MyFragment1() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.main_tab_home,container,false);
ib_update=(ImageButton) view.findViewById(R.id.ib_update);
ib_update.setOnClickListener(this);
return view;
}

@Override
public void onClick(View v) {
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
switch (v.getId()) {
case R.id.ib_update: //按钮
ft.replace(R.id.ll_home,new TheLatestFragment());
ft.commit();
break;

}
}
}

MyFragment1类的布局文件:
<?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"
android:id="@+id/ll_home">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#FF0000"
android:gravity="center"
android:text="@string/tab_menu_home"
android:textSize="20sp" />

</LinearLayout>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="221dip">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="210dp" >

<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="200dp" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="35dip"
android:layout_gravity="bottom"
android:background="#33000000"
android:gravity="center"
android:orientation="vertical" >

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text_image"
android:textColor="@android:color/white" />

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dip"
android:orientation="horizontal" >

<View
android:id="@+id/dot_0"
android:layout_width="5dip"
android:layout_height="5dip"
android:layout_marginLeft="2dip"
android:layout_marginRight="2dip"
android:background="@drawable/dot_focused"/>

<View
android:id="@+id/dot_1"
android:layout_width="5dip"
android:layout_height="5dip"
android:layout_marginLeft="2dip"
android:layout_marginRight="2dip"
android:background="@drawable/dot_normal"/>
<View
android:id="@+id/dot_2"
android:layout_width="5dip"
android:layout_height="5dip"
android:layout_marginLeft="2dip"
android:layout_marginRight="2dip"
android:background="@drawable/dot_normal"/>
<View
android:id="@+id/dot_3"
android:layout_width="5dip"
android:layout_height="5dip"
android:layout_marginLeft="2dip"
android:layout_marginRight="2dip"
android:background="@drawable/dot_normal"/>
<View
android:id="@+id/dot_4"
android:layout_width="5dip"
android:layout_height="5dip"
android:layout_marginLeft="2dip"
android:layout_marginRight="2dip"
android:background="@drawable/dot_normal"/>
</LinearLayout>
</LinearLayout>
</FrameLayout>
</RelativeLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="74dp"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<RadioGroup
android:id="@+id/rg_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:id="@+id/ib_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/button_culture"/>
<ImageButton
android:id="@+id/ib_cuture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/button_phone"/>
<ImageButton
android:id="@+id/ib_team"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/button_update"/>
</RadioGroup>
</LinearLayout>
</LinearLayout>
TheLatestFragment类和布局,我就不贴出来了
...全文
273 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
day_moon 2016-01-08
  • 打赏
  • 举报
回复
嗯。我觉得跟viewpage的容器有关,每点击底部的菜单按钮,就切换一个fragment,他们是放在容器中的,但是我定义的最新动态布局没有放进容器里面,只是替换一下界面。容器就默认在该布局文件的底部加载最新动态的布局。也就是要把最新动态的布局放到viewpage里面才可以,实现的时候就不会覆盖底部的菜单了。
day_moon 2016-01-07
  • 打赏
  • 举报
回复
为最新动态按钮添加点击事件
public void onClick(View v) {
Intent intent = new Intent(getActivity(), TheLatestFragment.class);
startActivity(intent);
}
是跳过去了,但是底部的菜单又没有了?


本人是菜鸟,希望大神耐心指教!

哎,真难 2016-01-07
  • 打赏
  • 举报
回复
fragment大小改变了,就用activity了
MoneyCitys 2016-01-07
  • 打赏
  • 举报
回复
哦 我看懂了 現在你是想要圖片標題接在首頁下方? 正常來說, 如果你沒去動layout 上一個fragment的位置在哪, 下一個的位置就在哪 那就要檢查你的TheLatestFragment的佈局 看是不是長度定死, 內容置中 或者其他之類的問題 若覺得檢查麻煩, 更直接的方法直接在rooview加上backgroundcolor 這樣就很清楚的知道為什麼你的圖片標題在那麼下面 不然全都白色的誰也不知道上面白白的一片是不是還有什麼元件
day_moon 2016-01-07
  • 打赏
  • 举报
回复
上面不是有效果图吗(我想要的结果)?

点击最新动态的按钮之后,当前的页面被最新动态的fragment(但是底部的菜单是可以看得到的)替换,我用了replace方法
代码如下:
ft.replace(R.id.ll_home,new TheLatestFragment());
ft.commit();
运行的结果:

位置不对啊,不符合我想要的结果啊?跪求大神解决!!!
MoneyCitys 2016-01-07
  • 打赏
  • 举报
回复
其實我看不太懂想要表達的意思 是切換畫面但底部的菜單不見了嗎? 若是, 就把底部菜單獨立出來別跟跳轉畫面的fragment放在一起 通常拉到activity裡面 切換畫面就不用再startactivity 直接使用replace就好
day_moon 2016-01-06
  • 打赏
  • 举报
回复
急!!!跪求大神回答!
day_moon 2016-01-06
  • 打赏
  • 举报
回复
我是想替换一个分页而已,没跳错吧。底部的布局是按钮组啊。
day_moon 2016-01-06
  • 打赏
  • 举报
回复
哦。如果我要实现上面的功能,那该如何修改?
哎,真难 2016-01-06
  • 打赏
  • 举报
回复
你没有理解fragment的概念,fragment是指一个内容,替换的是activity里面这个fragment的内容
small_technical 2016-01-05
  • 打赏
  • 举报
回复
你那跳转之后好像把下面导航也替换了,你跳转的位置是跳转到哪儿去了

80,472

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧