我想将FrameView放在工具栏上,如下图所示-
您会看到FrameView中的RecyclerView与工具栏重叠
但是我得到了这个,如下图所示-
在Android Studio预览中,其正确显示,这意味着框架布局在这样的工具栏上-
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/dim_50">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<ImageButton
android:id="@+id/backButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_navigate_before_black_24dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<ImageView
android:id="@+id/topicLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone" />
<TextView
android:id="@+id/txtLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Quiz"
android:textColor="@android:color/white"
android:textSize="@dimen/dim_19"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:background="@android:color/transparent">
</FrameLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity implements BaseFragment.OnBaseFragListener {
Toolbar toolbar;
TextView txtLabel;
ImageButton backButton;
ImageView topicLogo;
int navHomeVisibility = 1;
int navAboutVisibility = 1;
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
ViewGroup.MarginLayoutParams labelParams;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
txtLabel = findViewById(R.id.txtLabel);
backButton = findViewById(R.id.backButton);
topicLogo = findViewById(R.id.topicLogo);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
sharedPreferences = getSharedPreferences("preferences", MODE_PRIVATE);
editor = sharedPreferences.edit();
editor.clear();
editor.apply();
backButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
AddFrag(new ChooseTopicFragment(), 0, null);
labelParams = (ViewGroup.MarginLayoutParams) txtLabel.getLayoutParams();
}
public void setTitleMargin() {
labelParams.setMargins((int) getResources().getDimension(R.dimen.dim_10), 0, 0, 0);
}
public void unsetTitleMargin() {
labelParams.setMargins(0, 0, 0, (int) getResources().getDimension(R.dimen.dim_1));
}
public void setFragTitle(String FragTitle) {
txtLabel.setText(FragTitle);
}
public void setFragLogo(int FragLogo) {
topicLogo.setVisibility(View.VISIBLE);
topicLogo.setImageResource(FragLogo);
}
public void unsetFragLogo() {
topicLogo.setVisibility(View.GONE);
}
public void unsetMenuItems(int navHomeFlag) {
navHomeVisibility = navHomeFlag;
invalidateOptionsMenu();
backButton.setVisibility(View.GONE);
txtLabel.setTextSize(20);
}
public void setMenuItems(int navHomeFlag) {
navHomeVisibility = navHomeFlag;
invalidateOptionsMenu();
backButton.setVisibility(View.VISIBLE);
txtLabel.setTextSize(19);
}
public void unsetAbout(int navAboutFlag) {
navAboutVisibility = navAboutFlag;
invalidateOptionsMenu();
}
public void setAbout(int navAboutFlag) {
navAboutVisibility = navAboutFlag;
invalidateOptionsMenu();
}
public void unsetOnlyBackBtn() {
backButton.setVisibility(View.INVISIBLE);
}
public void AddFrag(Fragment fragment, int flag, String fragName) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
if (flag == 0) {
ft.add(R.id.container, fragment, fragName); //for adding ChooseTopic frag
} else if (flag == 1) {
ft.replace(R.id.container, fragment, fragName);
ft.addToBackStack(fragName);
} else if (flag == 2) {
ft.replace(R.id.container, fragment, fragName); //skips adding Questions frag to backstack
} else if (flag == 3) {
ft.remove(fragment); //removes Result frag after pressing back
}
ft.commitAllowingStateLoss();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem navHome = menu.findItem(R.id.navHome);
if (navHomeVisibility == 0)
navHome.setVisible(false);
else
navHome.setVisible(true);
MenuItem navAbout = menu.findItem(R.id.navAbout);
if (navAboutVisibility == 0)
navAbout.setVisible(false);
else
navAbout.setVisible(true);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.navHome) {
finishAffinity();
startActivity(new Intent(getApplicationContext(), MainActivity.class));
} else if (item.getItemId() == R.id.navAbout) {
AlertDialog.Builder aboutDialog = new AlertDialog.Builder(this);
aboutDialog.setTitle("About");
aboutDialog.setMessage(getResources().getText(R.string.about_desc));
aboutDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
aboutDialog.show();
}
return true;
}
@Override
public void onAttachFragment(Fragment fragment) {
if (fragment instanceof BaseFragment) {
BaseFragment baseFragment = (BaseFragment) fragment;
baseFragment.setOnBaseFragListener(this);
}
}
}
ChooseTopicFragment
public class ChooseTopicFragment extends BaseFragment {
private ArrayList<TopicModal> arrTopicModal = new ArrayList<>();
public ChooseTopicFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_choose_topic, container, false);
arrTopicModal.clear();
callback.unsetMenuItems(0);
callback.setAbout(1);
callback.unsetFragLogo();
callback.setFragTitle("Programming Quiz");
RecyclerView recyclerView = view.findViewById(R.id.recyclerView);
DBHelper dbHelper = DBHelper.getDB(getActivity());
if (!dbHelper.checkDB()) {
dbHelper.createDB(getActivity());
}
dbHelper.openDB();
ArrayList<Integer> arrBeg = new ArrayList<>();
ArrayList<Integer> arrInt = new ArrayList<>();
ArrayList<Integer> arrExp = new ArrayList<>();
for (int i = 1; i < 11; i++) {
arrBeg.add(dbHelper.getCount("Beginner", i));
arrInt.add(dbHelper.getCount("Intermediate", i));
arrExp.add(dbHelper.getCount("Expert", i));
}
addTopics(1, R.drawable.ic_java, "Java", arrBeg.get(0), arrInt.get(0), arrExp.get(0), getResources().getColor(android.R.color.holo_blue_dark));
addTopics(2, R.drawable.ic_c, "C", arrBeg.get(1), arrInt.get(1), arrExp.get(1), getResources().getColor(android.R.color.holo_red_light));
addTopics(3, R.drawable.ic_cpp, "C++", arrBeg.get(2), arrInt.get(2), arrExp.get(2), getResources().getColor(android.R.color.holo_orange_light));
addTopics(4, R.drawable.ic_android, "Android", arrBeg.get(3), arrInt.get(3), arrExp.get(3), getResources().getColor(android.R.color.holo_green_dark));
addTopics(5, R.drawable.ic_php, "PHP", arrBeg.get(4), arrInt.get(4), arrExp.get(4), getResources().getColor(android.R.color.holo_orange_dark));
addTopics(6, R.drawable.ic_css, "CSS", arrBeg.get(5), arrInt.get(5), arrExp.get(5), getResources().getColor(android.R.color.holo_purple));
addTopics(7, R.drawable.ic_html, "HTML", arrBeg.get(6), arrInt.get(6), arrExp.get(6), getResources().getColor(android.R.color.holo_orange_light));
addTopics(8, R.drawable.ic_python, "Python", arrBeg.get(7), arrInt.get(7), arrExp.get(7), getResources().getColor(android.R.color.holo_blue_dark));
addTopics(9, R.drawable.ic_javascript, "Javascript", arrBeg.get(8), arrInt.get(8), arrExp.get(8), getResources().getColor(android.R.color.holo_red_light));
addTopics(10, R.drawable.ic_kotlin, "Kotlin", arrBeg.get(9), arrInt.get(9), arrExp.get(9), getResources().getColor(android.R.color.holo_green_dark));
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
TopicAdapter adapter = new TopicAdapter(getActivity(), arrTopicModal);
recyclerView.setAdapter(adapter);
return view;
}
public void addTopics(int topicID, int image, String name, int beginner, int intermediate, int expert, int topicColor) {
TopicModal topicModal = new TopicModal();
topicModal.topicID = topicID;
topicModal.image = image;
topicModal.name = name;
topicModal.beginner = beginner;
topicModal.intermediate = intermediate;
topicModal.expert = expert;
topicModal.topicColor = topicColor;
arrTopicModal.add(topicModal);
}
}
答案 0 :(得分:0)
这不是最优雅的方法,但是您可以将不同视图上的高程设置为强制在最上方。我也相信在框架布局中,在xml中添加布局的顺序在导致问题的原因中起着重要的作用