一种布局可行但其他布局无效

时间:2017-03-27 15:20:06

标签: android android-layout android-studio android-fragments

大家。我正在创建一个简单的应用程序以获得乐趣并想出了这个问题。基本上,我检查了一切,发现布局有些问题。我使用导航和导航,它需要用户4个不同的部分。一切都有效,除了最后一部分,按钮不会把我带到任何地方。

import android.content.Intent;
import android.support.v4.app.Fragment;


import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

           DrawerLayout drawer = (DrawerLayout)    
findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open,   
R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView)    
findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

}




@Override
public void onBackPressed() {
    if (getFragmentManager().getBackStackEntryCount() > 0) {
        getFragmentManager().popBackStack();
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is    
present.
    getMenuInflater().inflate(R.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

    return super.onOptionsItemSelected(item);
}



private void displaySelectedScreen(int id)
{
    Fragment fragment = null;
    switch(id){
        case R.id.syllabus:
            fragment = new Syllabus();
            break;
        case R.id.questionss:
            fragment = new Questions();
            break;
        case R.id.definitions:
            fragment = new Definitions();
            break;
        case R.id.contact_us:
            fragment = new Contact_Us();
            break;
    }

    if(fragment != null){

        FragmentTransaction ft =    
getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.list_content,fragment).addToBackStack("tag");
        ft.commit();
    }




}

public void chaptersdefinitions(View view) {
    Fragment newfragment = null;
    if (view == findViewById(R.id.chap1def)) {
        newfragment = new Chapter1def();
    } else if (view == findViewById(R.id.chap2def)) {
        newfragment = new Chapter2def();
    } else if (view == findViewById(R.id.chap3def)) {
        newfragment = new Chapter3def();
    } else if (view == findViewById(R.id.chap4def)) {
        newfragment = new Chapter4def();
    } else if (view == findViewById(R.id.chap5def)) {
        newfragment = new Chapter5def();
    } else if (view == findViewById(R.id.chap6def)) {
        newfragment = new Chapter6def();
    } else if (view == findViewById(R.id.chap7def)) {
        newfragment = new Chapter7def();
    } else if (view == findViewById(R.id.chap8def)) {
        newfragment = new Chapter8def();
    } else if (view == findViewById(R.id.chap9def)) {
        newfragment = new Chapter9def();
    } else if (view == findViewById(R.id.chap10def)) {
        newfragment = new Chapter10def();

    }  else if (view == findViewById(R.id.chapone)) {
        newfragment = new Chapter1();
    } else if (view == findViewById(R.id.chaptwo)) {
        newfragment = new Chapter2();
    } else if (view == findViewById(R.id.chapthree)) {
        newfragment = new Chapter3();
    } else if (view == findViewById(R.id.chapfour)) {
        newfragment = new Chapter4();
    } else if (view == findViewById(R.id.chapfive)) {
        newfragment = new Chapter5();
    } else if (view == findViewById(R.id.chapsix)) {
        newfragment = new Chapter6();
    } else if (view == findViewById(R.id.chapseven)) {
        newfragment = new Chapter7();
    } else if (view == findViewById(R.id.chapeight)) {
        newfragment = new Chapter8();
    } else if (view == findViewById(R.id.chapnine)) {
        newfragment = new Chapter9();
    } else if (view == findViewById(R.id.chapten)) {
        newfragment = new Chapter10();
    }
    if (newfragment != null) {

        FragmentTransaction ft =      
getSupportFragmentManager().beginTransaction();
            ft.replace(R.id.list_content,      
newfragment).addToBackStack("tag");
            ft.commit();

        }
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {

        int id = item.getItemId();

        DrawerLayout drawer = (DrawerLayout)               
findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);

        displaySelectedScreen(id);
        return true;
    }
}

我的content_main是:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout    
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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.weebly.dunyaibdp.chemistryforib.MainActivity"
tools:showIn="@layout/app_bar_main"
android:fitsSystemWindows="true">

<android.support.constraint.ConstraintLayout
    android:id="@+id/list_content"
    android:layout_width="451dp"
    android:layout_height="600dp"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.885"
    app:layout_constraintHorizontal_bias="0.493">
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:text="Hey There!"
        android:textColor="#64dd17"
        android:textSize="50sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.502"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.237" />

       <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:text="Welcome to our Chemistry App"
        android:textColor="@android:color/holo_green_dark"
        android:textSize="25sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.413"
        app:layout_constraintHorizontal_bias="0.483" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:fontFamily="monospace"
        android:text="Use navigation on the left to navigate through app"
        android:textColor="@android:color/holo_green_dark"
        android:textSize="12sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.558"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.53" />

    </android.support.constraint.ConstraintLayout>

</android.support.constraint.ConstraintLayout>

在导航中我有教学大纲课程:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

<ScrollView
    android:id="@+id/scrollSyllabus"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:ignore="UselessParent">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">


            <Button
                android:id="@+id/chapone"
                android:layout_width="340sp"
                android:layout_height="70sp"
                android:layout_gravity="center"
                android:layout_marginLeft="55sp"
                android:layout_marginTop="50sp"
                android:background="@drawable/roundshapebtn"
                android:onClick="chaptersdefinitions"
                android:text="@string/stoichometric_relations1"
                android:textColor="#000000"
                tools:ignore="RtlHardcoded" />


        </RelativeLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>

然后我的课程大纲是布局是:

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;



public class Chapter1 extends Fragment {
    @Nullable
    @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup    
container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.chapteronesyll,container,false);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    getActivity().setTitle("Stoichometric Relations");
}
}

使用这个类,我使用这个布局来显示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
<Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="one" />
</LinearLayout>

现在对于这个按钮我没有放任何onclick所以我只想让这个按钮显示在屏幕上,就是这样。我在我的活动中尝试了一些其他布局,其中一些工作完美,但有些没有我试图删除并再次添加但这没有工作它仍然没有在屏幕上打印任何东西。任何帮助将不胜感激。

0 个答案:

没有答案