我遇到了FloatingActionButton的一些問題,它在仿真器上按照需要工作,但是當我安裝到我的手機(Galaxy s4,Android V4.4.4)時,它無法正常工作,我附加了一些圖片來說明錯誤。有人能告訴我我能做什麼嗎?FloatingActionButton無法正常工作
我把視圖中的片段
片段類
public class MiniGameMenuFragment extends Fragment implements View.OnClickListener {
protected FloatingActionButton more_menu_btn, card_view_mode_btn, expand_view_btn;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.mini_menu_fragment, container, false);
this.more_menu_btn = (FloatingActionButton) view.findViewById(R.id.more_c_btn);
this.card_view_mode_btn = (FloatingActionButton) view.findViewById(R.id.card_view_mode_c_btn);
this.expand_view_btn = (FloatingActionButton) view.findViewById(R.id.expand_view_c_btn);
this.more_menu_btn.setOnClickListener(this);
this.card_view_mode_btn.setOnClickListener(this);
this.expand_view_btn.setOnClickListener(this);
return view;
}
@Override
public void onClick(View v) {
}
}
mini_menu_fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/red">
<android.support.design.widget.FloatingActionButton
android:id="@+id/more_c_btn"
android:layout_width="38dp"
android:layout_height="38dp"
android:clickable="true"
android:scaleType="center"
android:src="@mipmap/ic_more_vert_white_24dp"
app:backgroundTint="#ff9800"
app:borderWidth="0dp"
app:elevation="8dp"
app:fabSize="normal"
app:pressedTranslationZ="12dp"
app:rippleColor="#ff5722" />
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/card_view_mode_c_btn"
android:layout_width="38dp"
android:layout_height="38dp"
android:clickable="true"
android:src="@mipmap/card_expanded_view_mode"
app:backgroundTint="#ff9800"
app:borderWidth="0dp"
app:elevation="8dp"
app:pressedTranslationZ="12dp"
app:rippleColor="#ff5722" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/expand_view_c_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:clickable="true"
android:src="@mipmap/zoom_out_white"
app:backgroundTint="#ff9800"
app:borderWidth="0dp"
app:elevation="8dp"
app:fabSize="normal"
app:pressedTranslationZ="12dp"
app:rippleColor="#ff5722" />
</LinearLayout>
主要活動XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/gameBackground"
android:orientation="vertical"
tools:context="com.example.MainActivity">
<RelativeLayout
android:id="@+id/floating_menu_container_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true">
</RelativeLayout>
</RelativeLayout>
MainActivity類別
public class MainActivity extends AppCompatActivity {
FragmentManager fragmentManager;
FragmentTransaction transaction;
MiniGameMenuFragment miniGameMenu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.miniGameMenu = new MiniGameMenuFragment();
this.fragmentManager = getFragmentManager();
this.transaction = fragmentManager.beginTransaction();
this.transaction.add(R.id.floating_menu_container_layout, miniGameMenu, "miniGameMenuFragment");
this.transaction.commit();
}
@Override
protected void onResumeFragments() {
super.onResumeFragments();
}
}
在模擬器
在電話銀河4S
解決:
的問題是海拔,如@SadiqMdAsif說8DP是太過分了我的星系S4分辨率這就是爲什麼它創造了一個額外的餘量,使我減少到2DP
兩種設備的分辨率都不相同。你應該爲不同的屏幕尺寸分開佈局。 –
@SadiqMdAsif那麼我該如何解決它?保證金爲負?因爲您可以看到保證金不是問題 – kobbycoder
請參閱下面的答案以創建多個佈局變體。您將低分辨率設備中的「顯示像素」設置爲「DP」,它們的像素少於高像素。所以10DP會在較低分辨率的設備上佔用更多空間。 –