layout.setBackgroundColor(color),直接在colorl里面设置透明度

本文详细介绍了如何使用layout.setBackgroundColor(color)方法在指定颜色中设置透明度,适用于Android开发中的布局背景颜色定制。
layout.setBackgroundColor(color),直接在colorl里面设置透明度
请帮我优化以下代码: 主布局文件:<?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" xmlns:tool="http://schemas.android.com/tools" android:orientation="vertical" tools:context=".Fragment.Fragment_main"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.viewpager.widget.ViewPager android:id="@+id/myviewpage" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/view" android:background="#f0f0f0"/> <View android:id="@+id/view" android:layout_width="wrap_content" android:layout_height="1dp" android:layout_above="@+id/bot_z" android:background="#f0f0f0" tool:layout_editor_absoluteX="419dp" tool:layout_editor_absoluteY="39dp" /> <LinearLayout android:id="@+id/bot_z" android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignParentBottom="true"> <TextView android:id="@+id/T1" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="首页" android:textSize="20sp" android:textStyle="bold" /> <TextView android:id="@+id/T2" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="订单" android:textSize="20sp" android:textStyle="bold" /> <TextView android:id="@+id/T3" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="我的" android:textSize="20sp" android:textStyle="bold" /> </LinearLayout> </RelativeLayout> </LinearLayout> 分布局文件:lout1:<?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" android:orientation="vertical"> <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:src="@drawable/mm001" /> </LinearLayout> lout2:<?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:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:src="@drawable/mm001"/> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="zhehsi page2" /> </LinearLayout> lout3:<?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:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:src="@drawable/mm001" /> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="zhehsi page3" /> </LinearLayout> 主main.java:package com.example.myapplication.Fragment; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentTransaction; import androidx.viewpager.widget.ViewPager; import com.example.myapplication.R; import com.example.myapplication.Viewpagerhd.Mypageradapter; import java.util.ArrayList; import java.util.List; public class Fragment_main extends AppCompatActivity implements View.OnClickListener { private Fragment fragment1, fragment2, fragment3; private TextView tab1, tab2, tab3; private ViewPager myViewpager; private List<Fragment> fragmentList; private Mypageradapter mypageradapter; private Fragment currentFragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fragment1); initUI(); // initFragments(); initTab(); } private void initTab() { Fragmenta_1 fragmenta_1 = new Fragmenta_1(); Fragmenta_2 fragmenta_2 = new Fragmenta_2(); Fragmenta_3 fragmenta_3 = new Fragmenta_3(); fragmentList = new ArrayList<Fragment>(); fragmentList.add(fragmenta_1); fragmentList.add(fragmenta_2); fragmentList.add(fragmenta_3); mypageradapter = new Mypageradapter(getSupportFragmentManager(),fragmentList); myViewpager .setAdapter(mypageradapter); myViewpager.addOnAdapterChangeListener(new MyPageChangeListennr()); showFragment(0); } private void showFragment(int i) { myViewpager.setCurrentItem(i); if (i == 0){ tab1.setBackgroundColor(Color.RED); tab2.setBackgroundColor(Color.WHITE); tab3.setBackgroundColor(Color.WHITE); }else if (i==1){ tab2.setBackgroundColor(Color.RED); tab1.setBackgroundColor(Color.WHITE); tab3.setBackgroundColor(Color.WHITE); }else if (i==2){ tab3.setBackgroundColor(Color.RED); tab2.setBackgroundColor(Color.WHITE); tab1.setBackgroundColor(Color.WHITE); } } public class MyPageChangeListennr implements ViewPager.OnPageChangeListener { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { if (position==0){ tab1.setBackgroundColor(Color.RED); tab2.setBackgroundColor(Color.WHITE); tab3.setBackgroundColor(Color.WHITE); }else if (position==1){ tab2.setBackgroundColor(Color.RED); tab1.setBackgroundColor(Color.WHITE); tab3.setBackgroundColor(Color.WHITE); }else if (position==2){ tab3.setBackgroundColor(Color.RED); tab2.setBackgroundColor(Color.WHITE); tab1.setBackgroundColor(Color.WHITE); } } @Override public void onPageScrollStateChanged(int state) { } } private void initUI() { tab1 = findViewById(R.id.T1); tab2 = findViewById(R.id.T2); tab3 = findViewById(R.id.T3); tab1.setOnClickListener(this); tab2.setOnClickListener(this); tab3.setOnClickListener(this); myViewpager =(ViewPager) findViewById(R.id.myviewpage); // updateTabColors(R.id.T1); } // private void initFragments() { // FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); // // // 按顺序添加所有Fragment // fragment1 = new Fragmenta_1(); // fragment2 = new Fragmenta_2(); // fragment3 = new Fragmenta_3(); // // transaction.add(R.id.view_pager, fragment1, "1"); // transaction.add(R.id.view_pager, fragment2, "2"); // transaction.add(R.id.view_pager, fragment3, "3"); // // transaction.hide(fragment2); // transaction.hide(fragment3); // transaction.commitNow(); // 立即提交事务 // // currentFragment = fragment1; // } @Override public void onClick(View v) { if (v.getId()==R.id.T1) { showFragment(0); }else if (v.getId()==R.id.T2){ showFragment(1); }else if (v.getId()==R.id.T3){ showFragment(2); } } private void switchFragment(Fragment targetFragment, int tabId) { if (targetFragment == currentFragment) return; FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out); transaction.hide(currentFragment); transaction.show(targetFragment); transaction.commitNow(); // 使用立即提交 currentFragment = targetFragment; updateTabColors(tabId); } private void updateTabColors(int selectedTabId) { int defaultColor = Color.WHITE; int selectedColor = Color.RED; tab1.setBackgroundColor(selectedTabId == R.id.T1 ? selectedColor : defaultColor); tab2.setBackgroundColor(selectedTabId == R.id.T2 ? selectedColor : defaultColor); tab3.setBackgroundColor(selectedTabId == R.id.T3 ? selectedColor : defaultColor); } } 适配器java: package com.example.myapplication.Fragment; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentPagerAdapter; import java.util.List; public class MypageAdaper extends FragmentPagerAdapter { private List<Fragment> fragmentList; public MypageAdaper(FragmentManager fm) { super(fm); } public MypageAdaper(FragmentManager fm,List<Fragment>fragmentList) { super(fm); this.fragmentList=fragmentList; } @Override public Fragment getItem(int position) { return fragmentList.get(position); } @Override public int getCount() { return fragmentList.size(); } } Fragmenta_1. java:package com.example.myapplication.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import androidx.fragment.app.Fragment; import com.example.myapplication.R; public class Fragmenta_1 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ View view = inflater.inflate(R.layout.lout1,container,false); return view; } } Fragmenta_2 .java:package com.example.myapplication.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import androidx.fragment.app.Fragment; import com.example.myapplication.R; public class Fragmenta_2 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ View view = inflater.inflate(R.layout.lout2,container,false); return view; } } Fragmenta_3.java:package com.example.myapplication.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import androidx.fragment.app.Fragment; import com.example.myapplication.R; public class Fragmenta_3 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ View view = inflater.inflate(R.layout.lout3,container,false); return view; } } 报错:错误: 无法将类 Mypageradapter中的构造器 Mypageradapter应用到给定类型; 需要: List<View> 找到: FragmentManager,List<Fragment> 原因: 实际参数列表和形式参数列表长度不同 错误: 不兼容的类型: Fragment_main.MyPageChangeListennr无法转换为OnAdapterChangeListener
05-31
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值