其他活动中可见的文字,

时间:2016-03-23 19:05:09

标签: android text textview fragment

我只需要一些快速帮助。我有一个导航抽屉设置,我使用活动主页作为我的欢迎屏幕,所以我在activity_main上放了一个文本框,但问题是,当我选择其他导航抽屉页面时,我会看到活动主页中的文本框。

我的活动页面应该是什么样的:http://puu.sh/nQFkc/1ed83811cc.png

结果如下:http://puu.sh/nQF93/785c9eee45.png

需要提及我对android的新手?

主要活动

package nota.outlawsindex;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {

private String[] mNavigationDrawerItemTitles;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
Toolbar toolbar;
Scrollview scrollView;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
    android.support.v7.app.ActionBarDrawerToggle mDrawerToggle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mTitle = mDrawerTitle = getTitle();
    mNavigationDrawerItemTitles =         getResources().getStringArray(R.array.navigation_drawer_items_array);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);
    ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView);


    setupToolbar();

    DataModel[] drawerItem = new DataModel[4];//need to update this if you     add start counting at 0

    drawerItem[0] = new DataModel(R.drawable.ic_connect, "Calculate     Sentence");
    drawerItem[1] = new DataModel(R.drawable.ic_fixtures, "Felonies");
    drawerItem[2] = new DataModel(R.drawable.ic_table, "Misdemeanors");
    drawerItem[3] = new DataModel(R.drawable.ic_drawer, "Infractions");
    //add on to the list to create more pages
    getSupportActionBar().setDisplayHomeAsUpEnabled(false);
    getSupportActionBar().setHomeButtonEnabled(true);

    DrawerItemCustomAdapter adapter = new DrawerItemCustomAdapter(this, R.layout.list_view_item_row, drawerItem);
    mDrawerList.setAdapter(adapter);
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerLayout.setDrawerListener(mDrawerToggle);
    setupDrawerToggle();



}

private class DrawerItemClickListener implements ListView.OnItemClickListener {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        selectItem(position);
    }

}

private void selectItem(int position) {



    scrollView.setVisibility(View.GONE);

    Fragment fragment = null;

    switch (position) {
        case 0:
            fragment = new ConnectFragment();
            break;
        case 1:
            fragment = new FixturesFragment();
            break;
        case 2:
            fragment = new TableFragment();
            break;
        case 3:
            fragment = new SelfAddedFragment();
            break;
        //add more cases if you add more pages

        default:
            break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame,             fragment).commit();

        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(mNavigationDrawerItemTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);

    } else {
        Log.e("MainActivity", "Error in creating fragment");
    }
}

@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) {
    //
    int id = item.getItemId();
    //
    if (mDrawerToggle.onOptionsItemSelected(item)) {

        return true;
    } else if (id == R.id.action_settings) {
        Toast.makeText(getApplicationContext(), "Settings", Toast.LENGTH_SHORT).show();
        return true;
    }
    Toast.makeText(getApplicationContext(), "Search", Toast.LENGTH_SHORT).show();

    return super.onOptionsItemSelected(item);
}

@Override
public void setTitle(CharSequence title) {
    mTitle = title;
    getSupportActionBar().setTitle(mTitle);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();
}

void setupToolbar() {
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    //getSupportActionBar().setDisplayShowHomeEnabled(true);
}

void setupDrawerToggle() {
    mDrawerToggle = new android.support.v7.app.ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.app_name, R.string.app_name);
    //This is necessary to change the icon of the Drawer Toggle upon state change.
    mDrawerToggle.syncState();
}

}

Activity_Main

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/backgroundColor">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/relativeLayout"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <RelativeLayout
            android:id="@+id/container_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

        </RelativeLayout>


        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/container_toolbar"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true">

        </FrameLayout>

        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar"
            android:layout_gravity="right|top"
            android:layout_below="@+id/scrollView"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginLeft="26dp"
            android:layout_marginStart="26dp" />

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/scrollView"
            android:layout_centerVertical="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true">

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@+id/content_frame"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:id="@+id/relativeLayout2"
                android:focusable="false">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?         android:attr/textAppearanceSmall"
                    android:text="              Welcome to The Outlaw&apos;s                                                 Index! An app designed for individuals who want to educate themselves with knowledge regarding different types of offenses in the United States. Currently this app is directed towards individuals in the United States, but a Canadian version may be released in upcoming months. This app gives users the tools and resources to research different classes of offenses and the repercussions one would receive if convicted. Although this app is informative, advice from law enforcement, attorneys, and other members of the law should be considered more accurate as it may suit your personal needs regarding a specific case. To start off, choose any of the classes on the left hand side, or calculate a sentence and the repercussions that would follow."
                    android:id="@+id/textView2"
                    android:textColor="#ffffff"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp"
                    android:password="false"
                    android:phoneNumber="false"
                    android:singleLine="false"
                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="33dp" />

            </RelativeLayout>
        </ScrollView>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Welcome"
            android:id="@+id/textView"
            android:textColor="#ffffff"
            android:layout_above="@+id/scrollView"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginLeft="27dp"
            android:layout_marginStart="27dp" />

    </RelativeLayout>

</RelativeLayout>

<ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@color/menuBackgroundColor"
    android:choiceMode="singleChoice"
    android:divider="@color/colorAccent"
    android:dividerHeight="1dp"
    android:paddingTop="15dp"/>
<!--    because there is no header in this one I am using android:paddingTop="15dp"
    to push the menu below the level of the translucent top bar.-->

我的一个Navdrawer页面(与屏幕截图相同)

片段

package nota.outlawsindex;



import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class TableFragment extends Fragment {

Button MisdemeanorClassAButton = null;
Button MisdemeanorClassBButton = null;
Button MisdemeanorClassCButton = null;
Button MisdemeanorClassDButton = null;
TextView MisdemeanorText;


public TableFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_table, container, false);
    init(rootView);
    return rootView;

}


public void init(View view) {
    MisdemeanorClassAButton = (Button) view.findViewById(R.id.MisdemeanorClassAButton);
    MisdemeanorText = (TextView) view.findViewById(R.id.MisdemeanorText);
    MisdemeanorClassAButton.setOnClickListener(new View.OnClickListener() {
                                                public void onClick(View view) {
                                                    MisdemeanorText.setText(R.string.MA);
                                                }
                                            }
    );
    MisdemeanorClassBButton = (Button) view.findViewById(R.id.MisdemeanorClassBButton);
    MisdemeanorText = (TextView) view.findViewById(R.id.MisdemeanorText);
    MisdemeanorClassBButton.setOnClickListener(new View.OnClickListener() {
                                                   public void onClick(View view) {
                                                       MisdemeanorText.setText(R.string.MB);
                                                   }
                                               }
    );
    MisdemeanorClassCButton = (Button) view.findViewById(R.id.MisdemeanorClassCButton);
    MisdemeanorText = (TextView) view.findViewById(R.id.MisdemeanorText);
    MisdemeanorClassCButton.setOnClickListener(new View.OnClickListener() {
                                                   public void onClick(View view) {
                                                       MisdemeanorText.setText(R.string.MC);
                                                   }
                                               }
    );
    MisdemeanorClassDButton = (Button) view.findViewById(R.id.MisdemeanorClassDButton);
    MisdemeanorText = (TextView) view.findViewById(R.id.MisdemeanorText);
    MisdemeanorClassDButton.setOnClickListener(new View.OnClickListener() {
                                                   public void onClick(View view) {
                                                       MisdemeanorText.setText(R.string.MD);
                                                   }
                                               }
    );
}
}

其中一个NavDrawer页面的活动页面

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/backgroundColor">


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/MTB"
    android:id="@+id/MisdemeanorText"
    android:textColor="#ffffff"
    android:layout_row="6"
    android:layout_column="1"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Class A"
    android:id="@+id/MisdemeanorClassAButton"
    android:layout_marginTop="34dp"
    android:layout_row="0"
    android:layout_column="0"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Class B"
    android:id="@+id/MisdemeanorClassBButton"
    android:layout_row="0"
    android:layout_column="1"
    android:layout_alignTop="@+id/MisdemeanorClassAButton"
    android:layout_toRightOf="@+id/MisdemeanorClassAButton"
    android:layout_toEndOf="@+id/MisdemeanorClassAButton" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Class C"
    android:id="@+id/MisdemeanorClassCButton"
    android:layout_alignTop="@+id/MisdemeanorClassBButton"
    android:layout_toLeftOf="@+id/MisdemeanorClassDButton"
    android:layout_toStartOf="@+id/MisdemeanorClassDButton" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Class D"
    android:id="@+id/MisdemeanorClassDButton"
    android:layout_alignBottom="@+id/MisdemeanorClassCButton"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />


</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

问题在于您的布局。 解决问题的两种方法 -

  1. 每当您通过从导航栏中选择任何选项来实例化任何片段时,您可以将ScrollView(包含欢迎文本)的可见性设置为GONE

    Scrollview scrollView;

    @覆盖 protected void onCreate(Bundle savedInstanceState){     super.onCreate(savedInstanceState);

    ...

    scrollView =(ScrollView)findViewById(R.id.scrollView);

    }

    private void selectItem(int position){

  2. scrollView.setVisibility(View.GONE);

    ...

    }

    1. 使用您只显示欢迎文字的布局再创建一个片段,并在活动的onCreate()中启动此片段。因此,当用户启动您的应用时,这将显示欢迎文本。当用户从Navdrawer中选择任何项目时,您可以启动您选择的另一个片段。但为此,您应该从活动布局xml中删除欢迎文本。