标签:android style class blog code java
设置两个片段,昨天片段对右边进行颜色更改,片段替换的操作
点击片段1:改变片段1的颜色值
点击片段2替换片段1
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="match_parent" tools:context=".MainActivity" > <fragment android:id="@+id/fragment1" android:name="com.example.android_fragment.other.MyFragment1" android:layout_width="100dp" android:layout_height="match_parent" /> <fragment android:id="@+id/fragment2" android:name="com.example.android_fragment.other.MyFragment2" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_toRightOf="@+id/fragment1" /> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:paddingLeft="20dp" android:paddingTop="10dp" android:background="#DCB5FF"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="片段1" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="片段2" /> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="片段3" /> <TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="片段4" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ll_item" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFBFFF" android:orientation="vertical" android:paddingTop="10dp" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="TextView" /> </LinearLayout>
<?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="#FFE66F" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:paddingTop="10dp" android:text="替换的textview" /> </LinearLayout>
package com.example.android_fragment; import android.os.Bundle; import android.support.v4.app.FragmentActivity; /** * 主activity,包含2个片段 * @author zhaoyazhi * * 2014-6-13 */ public class MainActivity extends FragmentActivity { @Override protected void onCreate(Bundle arg0) { super.onCreate(arg0); setContentView(R.layout.activity_main); } }
package com.example.android_fragment.other; import android.app.Activity; import android.graphics.Color; 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.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.TextView; import com.example.android_fragment.R; /** * 左边片段 * @author zhaoyazhi * * 2014-6-13 */ public class MyFragment1 extends Fragment { private FragmentActivity activity; /** * 把activity造型为FragmentActivity */ @Override public void onAttach(Activity activity) { // TODO Auto-generated method stub super.onAttach(activity); this.activity = (FragmentActivity) activity; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //设置布局 View view = inflater.inflate(R.layout.fragment_list_item1, container, false); //查找控件并设置点击事件 TextView tv1 = (TextView) view.findViewById(R.id.textView1); tv1.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { //改变背景值 changeFragmentColor(); } }); //查找控件并设置点击事件 TextView tv2 = (TextView) view.findViewById(R.id.textView2); tv2.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { //改变整个Fragment changeFragment(); } }); return view; } //改变整个Fragment private void changeFragment() { // 1.获取FragmentManager对象 FragmentManager manager = getActivity() .getSupportFragmentManager(); // 2.获取fragment的事务操作 代表:activity对fragment执行的多个改变的操作 FragmentTransaction transaction = manager.beginTransaction(); // 添加替换或删除Fragment这时候就需要FragmentTransaction的布局动态文件 // 执行替换 //参数1:父元素的id值,参数2:替换新fragment对象 transaction.replace(R.id.fragment2, new MyFragment3()); // 3.提交事务 transaction.commit(); } //改变控件的颜色 private void changeFragmentColor() { // 1.获取FragmentManager对象,只要获取FragmentManager对象就能获取fragment对象 FragmentManager manager = getActivity() .getSupportFragmentManager(); // 2.根据FragmentManager对象的findFragmentById方法来获取指定的fragment Fragment fragment2 = manager.findFragmentById(R.id.fragment2); // 3.获取Fragment中的布局文件 View v = fragment2.getView(); // 4.获取view中任何控件 LinearLayout layout = (LinearLayout) v .findViewById(R.id.ll_item); // 5.改变颜色背景值 layout.setBackgroundColor(Color.YELLOW); } }
package com.example.android_fragment.other; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.example.android_fragment.R; /** * 右边片段 * @author zhaoyazhi * * 2014-6-13 */ public class MyFragment2 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_list_item2, container, false); return v; } }
package com.example.android_fragment.other; import com.example.android_fragment.R; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * 全部替换的右边片段 * @author zhaoyazhi * * 2014-6-13 */ public class MyFragment3 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_list_replace, container, false); } }
赵雅智_通过fragment对布局进行改变,布布扣,bubuko.com
标签:android style class blog code java
原文地址:http://blog.csdn.net/zhaoyazhi2129/article/details/30452785