点击底部标签切换布局

YXTS122 2017-03-30 06:00:55
MainActivity.java
package com.example.oneexample;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;

public class MainActivity extends FragmentActivity implements OnClickListener {
private LinearLayout ask,me;
private Fragment current;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ask=(LinearLayout)findViewById(R.id.ask);
me=(LinearLayout)findViewById(R.id.me);
ask.setOnClickListener(this);
me.setOnClickListener(this);
current=new Fragment();
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.ask:
LeftFragment fragment=new LeftFragment();
FragmentManager fragmentManager=getSupportFragmentManager();
FragmentTransaction transaction=fragmentManager.beginTransaction();
if (current==fragment)
break;
if(!fragment.isAdded())
{
transaction.hide(current);
transaction.add(R.id.cont, fragment);
transaction.commit();
}
else
{
transaction.hide(current);
transaction.show(fragment);
transaction.commit();
}
current=fragment;
break;
case R.id.me:
RightFragment fragments=new RightFragment();
FragmentManager fragmentManagers=getSupportFragmentManager();
FragmentTransaction transactions=fragmentManagers.beginTransaction();
if (current==fragments)
break;
if(!fragments.isAdded())
{
transactions.hide(current);
transactions.add(R.id.cont, fragments);
transactions.commit();
}
else
{
transactions.hide(current);
transactions.show(fragments);
transactions.commit();
}
current=fragments;
break;
default:break;
}
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

LeftFragment.java
package com.example.oneexample;

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

public class LeftFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState)
{
View view=inflater.inflate(R.layout.left_activity,container,false);
return view;
}

}

RightFragment.java
package com.example.oneexample;

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

public class RightFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState)
{
View view=inflater.inflate(R.layout.right_activity,container,false);
return view;
}

}

activity_main.xml
<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="com.example.oneexample.MainActivity"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/cont"
android:layout_width="match_parent"
android:layout_height="410dp">
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="54dp"
android:gravity="center_vertical"
android:orientation="horizontal"
android:baselineAligned="true">
<LinearLayout
android:id="@+id/ask"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_horizontal"
android:background="#ffc0cb"
android:orientation="vertical" >
<TextView
android:id="@+id/tv1"
android:gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/wenda"
android:textSize="17sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/me"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/wo"
android:textSize="17sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>

left_activity.xml
<?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:background="#00ff00" >


</LinearLayout>

right_activity.xml
<?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:background="#ffff00" >


</LinearLayout>

...全文
183 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
YXTS122 2017-05-07
  • 打赏
  • 举报
回复
<?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="wrap_content"
android:orientation="vertical" >

<RelativeLayout
android:layout_width="match_parent"
android:paddingBottom="8dp"
android:layout_height="120dp" >

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:paddingBottom="15dp"
android:src="@drawable/ic_launcher" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="未登录"
android:textSize="15sp" />
</RelativeLayout>

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@drawable/ic_launcher" />

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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:paddingBottom="10sp"
android:paddingLeft="20sp"
android:paddingTop="10sp"
android:text="订单管理"
android:textSize="20sp" />
</RelativeLayout>

</LinearLayout>


YXTS122 2017-05-06
  • 打赏
  • 举报
回复
<?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="80dp"
android:orientation="horizontal" >

<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@drawable/ic_launcher"
android:scaleType="fitXY" />

<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="床"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" ¥6000"
android:textSize="13sp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FF0000" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#00ff00"
android:text="确认"
android:textSize="13sp" />
<Button
android:id="@+id/bt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/bt1"
android:layout_alignBottom="@+id/bt1"
android:layout_marginRight="15dp"
android:layout_toLeftOf="@+id/bt1"
android:background="#00ff00"
android:text="取消"
android:textSize="13sp" />
</RelativeLayout>

</LinearLayout>
</LinearLayout>
</LinearLayout>


YXTS122 2017-05-05
  • 打赏
  • 举报
回复
<?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="wrap_content"
android:orientation="vertical" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingBottom="35dp"
android:paddingTop="25dp" >

<TextView
android:id="@+id/tv5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableTop="@drawable/ic_launcher"
android:gravity="center"
android:text="第一个" />

<TextView
android:id="@+id/apple"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:drawableTop="@drawable/ic_launcher"
android:gravity="center"
android:text="第二个" />
</LinearLayout>


</LinearLayout>


ink_s 2017-03-30
  • 打赏
  • 举报
回复

80,351

社区成员

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

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