一、前言 昨天完成了加載頁,星球頁的設計,學習了Fragment實現(xiàn)底部導航欄。 今天將Cardview與傳感器結合起來,實現(xiàn)了發(fā)布表可以跟隨手機晃動。學習了Fragment的嵌套實現(xiàn)了我的頁面上“我的發(fā)帖”與“我的回帖”的切換。遇到的困難:一些控件或者版本之間的沖突,通過查閱資料,修改包解決問題;Fragment沒有很好的理解,導致在嵌套中出現(xiàn)了困難,通過查閱資料與重學Fragment實現(xiàn)嵌套,UI頁面方面出現(xiàn)問題,不知如何具體下去。 明天對登錄注冊頁面進行美化,以及學習頭部標題欄的運用。 二、成果展示 ? ? ?三、代碼 SendActivity.java ? ![]() package com.example.myteamapplication.Activity; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.view.WindowManager; import android.widget.FrameLayout; import androidx.appcompat.app.AppCompatActivity; import androidx.cardview.widget.CardView; import com.example.myteamapplication.R; public class SendActivity extends AppCompatActivity implements SensorEventListener { private SensorManager sensorManager; private Sensor defaultSensor; private CardView cv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //去掉頂部標題 getSupportActionBar().hide(); //去掉最上面時間、電量等 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN , WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_send); initView(); sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);//獲得傳感器管理 defaultSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);//設置類型 } @Override protected void onResume() { super.onResume(); sensorManager.registerListener(this, defaultSensor, SensorManager.SENSOR_DELAY_GAME);//注冊傳感器 } @Override protected void onDestroy() { super.onDestroy(); sensorManager.unregisterListener(this);//注銷傳感器 } @Override public void onSensorChanged(SensorEvent event) { changeLocation(event.values[1], event.values[2]); } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } private void changeLocation(float y, float z) { FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) cv.getLayoutParams(); layoutParams.setMargins((int) z * 5, (int) y * 5, 0, 0);//乘2的作用是為了讓效果明顯點 cv.setLayoutParams(layoutParams); } private void initView() { cv = (CardView) findViewById(R.id.cv); } }View Code activity_send.xml ![]() <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas./apk/res/android" xmlns:app="http://schemas./apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/bg"> <androidx.cardview.widget.CardView android:id="@ id/cv" android:layout_width="300dp" android:layout_height="400dp" android:layout_gravity="center" app:cardCornerRadius="10dp" app:contentPadding="20dp" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:text="請選擇您發(fā)布內(nèi)容的類別:" /> <RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="40dp" android:gravity="center"> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="吐槽"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="表白"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="交友"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="其他"/> </RadioGroup> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="請輸入您要發(fā)布的內(nèi)容:" android:layout_marginTop="75dp"/> <EditText android:id="@ id/et_data_upass" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="80dp" /> </androidx.cardview.widget.CardView> <com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@ id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|center" app:srcCompat="@android:drawable/ic_input_add" android:layout_marginBottom="50dp"/> </FrameLayout>View Code Fragment_My.java ![]() package com.example.myteamapplication.Fragment; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.RadioButton; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentTransaction; import com.example.myteamapplication.Activity.MainActivity; import com.example.myteamapplication.Activity.SendActivity; import com.example.myteamapplication.R; public class Fragment_My extends Fragment implements View.OnClickListener { private FragmentTransaction transaction; private FragmentManager manager; private RadioButton my_tab_send,my_tab_receive; private Context MainActivity; private LayoutInflater inflater; // @Override // public void onActivityCreated(Bundle savedInstanceState) { // super.onActivityCreated(savedInstanceState); // MainActivity = getActivity(); // inflater = LayoutInflater.from(getActivity()); // } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); manager = getChildFragmentManager(); transaction = manager.beginTransaction(); transaction.add(R.id.fragment_container_my,new Fragment_My_send()); transaction.commit(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View rootView = inflater.inflate(R.layout.fragment_my, container, false); my_tab_send = rootView.findViewById(R.id.my_tab_send); my_tab_receive = rootView.findViewById(R.id.my_tab_receive); my_tab_send.setOnClickListener(this); my_tab_receive.setOnClickListener(this); return rootView; } @Override public void onClick(View v) { transaction = manager.beginTransaction(); switch (v.getId()){ case R.id.my_tab_send: transaction.replace(R.id.fragment_container_my,new Fragment_My_send()); break; case R.id.my_tab_receive: transaction.replace(R.id.fragment_container_my,new Fragment_My_receive()); break; } transaction.commit(); } }View Code fragment_my.xml ![]() <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas./apk/res/android" xmlns:tools="http://schemas./tools" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@ id/fragment_container_my" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@ id/ll" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@ id/ll" android:background="#dcdcdc" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="用戶頭像和id和用戶名"/> <RadioGroup android:id="@ id/tabs_rg_my" android:layout_width="match_parent" android:layout_height="56dp" android:orientation="horizontal"> <RadioButton android:id="@ id/my_tab_send" style="@style/Custom.TabRadioButton" android:checked="true" android:drawableTop="@drawable/tab_sign_selector" android:text="我的發(fā)帖" /> <RadioButton android:id="@ id/my_tab_receive" style="@style/Custom.TabRadioButton" android:drawableTop="@drawable/tab_record_selector" android:text="我的回帖" /> </RadioGroup> </LinearLayout> </RelativeLayout>View Code 四、今日團隊鏈接 https://www.cnblogs.com/three3/p/12728120.html 來源:https://www./content-4-678851.html |
|