我在xoloQ600s和Honor5x中的fab按钮输出,我想我的fab按钮可以像所有设备一样工作。
下面是我的代码......
在我的主要布局中有fab按钮,当我点击fab按钮时,fab按钮菜单打开...........这样的菜单布局..
fab_layout.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end|right"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_menu_compass"
android:visibility="invisible"
app:backgroundTint="@color/colorFAB"
app:fabSize="mini" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end|right"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_menu_myplaces"
android:visibility="invisible"
app:backgroundTint="@color/colorFAB"
app:fabSize="mini" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end|right"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_menu_share"
android:visibility="invisible"
app:backgroundTint="@color/colorFAB"
app:fabSize="mini" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end|right"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_delete"
android:visibility="invisible"
app:backgroundTint="@color/colorFAB"
app:fabSize="mini" />
</FrameLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
FloatingActionButton fab;
FloatingActionButton fab1;
FloatingActionButton fab2;
FloatingActionButton fab3;
FloatingActionButton fab4;
CoordinatorLayout rootLayout;
//Save the FAB's active status
//false -> fab = close
//true -> fab = open
private boolean FAB_Status = false;
//Animations
Animation show_fab_1;
Animation hide_fab_1;
Animation show_fab_2;
Animation hide_fab_2;
Animation show_fab_3;
Animation hide_fab_3;
Animation show_fab_4;
Animation hide_fab_4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
rootLayout = (CoordinatorLayout) findViewById(R.id.coordinatorLayout);
//Floating Action Buttons
fab = (FloatingActionButton) findViewById(R.id.fab);
fab1 = (FloatingActionButton) findViewById(R.id.fab_1);
fab2 = (FloatingActionButton) findViewById(R.id.fab_2);
fab3 = (FloatingActionButton) findViewById(R.id.fab_3);
fab4 = (FloatingActionButton) findViewById(R.id.fab_4);
//Animations
show_fab_1 = AnimationUtils.loadAnimation(getApplication(), R.anim.fab1_show);
hide_fab_1 = AnimationUtils.loadAnimation(getApplication(), R.anim.fab1_hide);
show_fab_2 = AnimationUtils.loadAnimation(getApplication(), R.anim.fab2_show);
hide_fab_2 = AnimationUtils.loadAnimation(getApplication(), R.anim.fab2_hide);
show_fab_3 = AnimationUtils.loadAnimation(getApplication(), R.anim.fab3_show);
hide_fab_3 = AnimationUtils.loadAnimation(getApplication(), R.anim.fab3_hide);
show_fab_4 = AnimationUtils.loadAnimation(getApplication(), R.anim.fab4_show);
hide_fab_4 = AnimationUtils.loadAnimation(getApplication(), R.anim.fab4_hide);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (FAB_Status == false) {
//Display FAB menu
expandFAB();
FAB_Status = true;
} else {
//Close FAB menu
hideFAB();
FAB_Status = false;
}
}
});
fab1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplication(), "Floating Action Button 1", Toast.LENGTH_SHORT).show();
}
});
fab2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplication(), "Floating Action Button 2", Toast.LENGTH_SHORT).show();
}
});
fab3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplication(), "Floating Action Button 3", Toast.LENGTH_SHORT).show();
}
});
fab4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplication(), "Floating Action Button 3", Toast.LENGTH_SHORT).show();
}
});
//Initialize an empty list of 50 elements
List list = new ArrayList();
for (int i = 0; i < 50; i++) {
list.add(new Object());
}
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
Recycler_View_Adapter adapter = new Recycler_View_Adapter(list, getApplication());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (FAB_Status) {
hideFAB();
FAB_Status = false;
}
return false;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void expandFAB() {
//Floating Action Button 1
FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) fab1.getLayoutParams();
layoutParams.rightMargin += (int) (fab1.getWidth() * 1.5);
layoutParams.bottomMargin += (int) (fab1.getHeight() * 0.0);
fab1.setLayoutParams(layoutParams);
fab1.startAnimation(show_fab_1);
fab1.setClickable(true);
//Floating Action Button 2
FrameLayout.LayoutParams layoutParams2 = (FrameLayout.LayoutParams) fab2.getLayoutParams();
layoutParams2.rightMargin += (int) (fab2.getWidth() * 1.00);
layoutParams2.bottomMargin += (int) (fab2.getHeight() * 0.50);
fab2.setLayoutParams(layoutParams2);
fab2.startAnimation(show_fab_2);
fab2.setClickable(true);
//Floating Action Button 3
FrameLayout.LayoutParams layoutParams3 = (FrameLayout.LayoutParams) fab3.getLayoutParams();
layoutParams3.rightMargin += (int) (fab3.getWidth() * 0.50);
layoutParams3.bottomMargin += (int) (fab3.getHeight() * 1.0);
fab3.setLayoutParams(layoutParams3);
fab3.startAnimation(show_fab_3);
fab3.setClickable(true);
//Floating Action Button 3
FrameLayout.LayoutParams layoutParams4 = (FrameLayout.LayoutParams) fab4.getLayoutParams();
layoutParams4.rightMargin += (int) (fab4.getWidth() * 0.0);
layoutParams4.bottomMargin += (int) (fab4.getHeight() * 1.5);
fab4.setLayoutParams(layoutParams4);
fab4.startAnimation(show_fab_4);
fab4.setClickable(true);
}
private void hideFAB() {
//Floating Action Button 1
FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) fab1.getLayoutParams();
layoutParams.rightMargin -= (int) (fab1.getWidth() * 1.5);
layoutParams.bottomMargin -= (int) (fab1.getHeight() * 0.0);
fab1.setLayoutParams(layoutParams);
fab1.startAnimation(hide_fab_1);
fab1.setClickable(false);
//Floating Action Button 2
FrameLayout.LayoutParams layoutParams2 = (FrameLayout.LayoutParams) fab2.getLayoutParams();
layoutParams2.rightMargin -= (int) (fab2.getWidth() * 1.0);
layoutParams2.bottomMargin -= (int) (fab2.getHeight() * 0.50);
fab2.setLayoutParams(layoutParams2);
fab2.startAnimation(hide_fab_2);
fab2.setClickable(false);
//Floating Action Button 3
FrameLayout.LayoutParams layoutParams3 = (FrameLayout.LayoutParams) fab3.getLayoutParams();
layoutParams3.rightMargin -= (int) (fab3.getWidth() * 0.50);
layoutParams3.bottomMargin -= (int) (fab3.getHeight() * 1.0);
fab3.setLayoutParams(layoutParams3);
fab3.startAnimation(hide_fab_3);
fab3.setClickable(false);
//Floating Action Button 4
FrameLayout.LayoutParams layoutParams4 = (FrameLayout.LayoutParams) fab4.getLayoutParams();
layoutParams4.rightMargin -= (int) (fab4.getWidth() * 0.0);
layoutParams4.bottomMargin -= (int) (fab4.getHeight() * 1.5);
fab4.setLayoutParams(layoutParams4);
fab4.startAnimation(hide_fab_4);
fab4.setClickable(false);
}
}
当fab菜单打开和关闭时,它上面有一个自定义动画....
动画文件看起来像这样..
fab1_show.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<!-- Rotate -->
<rotate
android:duration="500"
android:fromDegrees="30"
android:interpolator="@android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="4"
android:repeatMode="reverse"
android:toDegrees="0"></rotate>
<!--Move-->
<translate
android:duration="1000"
android:fromXDelta="150%"
android:fromYDelta="0%"
android:interpolator="@android:anim/linear_interpolator"
android:toXDelta="0%"
android:toYDelta="0%"></translate>
<!--Fade In-->
<alpha
android:duration="2000"
android:fromAlpha="0.0"
android:interpolator="@android:anim/decelerate_interpolator"
android:toAlpha="1.0"></alpha>
</set>
fab1_hide.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<!--Move-->
<translate
android:duration="1000"
android:fromXDelta="-150%"
android:fromYDelta="-0%"
android:interpolator="@android:anim/linear_interpolator"
android:toXDelta="0%"
android:toYDelta="0%"></translate>
<!--Fade Out-->
<alpha
android:duration="2000"
android:fromAlpha="1.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:toAlpha="0.0"></alpha>
</set>
这是main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end|right"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_menu_help"
/>
<include layout="@layout/fab_layout" />
</android.support.design.widget.CoordinatorLayout>
答案 0 :(得分:0)
看起来您的设备都有不同的屏幕密度。为了在具有不同屏幕密度的所有设备上获得通用外观,您需要分别为每个密度括号(低,中,高,超高等)创建不同的布局文件副本。这些文件应根据其密度括号放置在不同的布局文件夹中,并且对边距或填充等具有不同的规范。
有关密度括号的更多信息以及创建这些布局文件夹和文件的步骤,请通过以下链接进行操作:
http://developer.android.com/guide/practices/screens_support.html