Android 如何使用按钮高亮显示文本?

Android 如何使用按钮高亮显示文本?,android,android-layout,android-fragments,android-linearlayout,android-button,Android,Android Layout,Android Fragments,Android Linearlayout,Android Button,我的代码中有一个导航抽屉。导航抽屉有按钮,可以在应用程序中导航。现在,当我单击按钮时,按钮会高亮显示。我期望的是,按钮会随着文本一起高亮显示,当我再次拉出导航抽屉时,它会显示最后一个高亮显示的按钮文本,直到我单击另一个按钮,该按钮在单击后高亮显示并保持不变高亮显示,直到做出另一个选择。以下是我的代码: public class NavigationPanelFragment extends Fragment implements OnClickListener { public sta

我的代码中有一个导航抽屉。导航抽屉有按钮,可以在应用程序中导航。现在,当我单击按钮时,按钮会高亮显示。我期望的是,按钮会随着文本一起高亮显示,当我再次拉出导航抽屉时,它会显示最后一个高亮显示的按钮文本,直到我单击另一个按钮,该按钮在单击后高亮显示并保持不变高亮显示,直到做出另一个选择。以下是我的代码:

public class NavigationPanelFragment extends Fragment implements OnClickListener {

    public static final String TAG_NAVIGATION_PANEL_FRAGMENT = "NavigationPanelFragment";
    public static final String ACTIVE_MENU_ITEM = "ActiveMenuItem";
    public final static String activeFragmentTitle = "";
    public static void newInstance(final FragmentManager manager, final String activeFragmentTag) {
        final NavigationPanelFragment fragment = new NavigationPanelFragment();
        final Bundle arguments = new Bundle();
        arguments.putString(NavigationPanelFragment.ACTIVE_MENU_ITEM, activeFragmentTag);
        fragment.setArguments(arguments);

        final FragmentInfo fragmentInfo = new FragmentInfo(TransactionMethods.ADD);
        fragmentInfo.setAnimation(R.anim.slide_in_from_left, FragmentInfo.NO_ANIMATION);
        fragmentInfo.setPopAnimation(0, R.anim.slide_out_to_left);
        fragmentInfo.setFragmentTag(TAG_NAVIGATION_PANEL_FRAGMENT);
        fragmentInfo.doNotAddToBackStack();
        fragmentInfo.setActionBarTitle(Application.getAppResources().getString(R.string.title_applications));
        FragmentStackManager.getInstance().transitionFragment(manager, fragment, fragmentInfo);
    }
@SuppressWarnings("deprecation")
@Override
public void onActivityCreated(final Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    highlightActiveMenuItem();
}
    private void highlightActiveMenuItem() {
        TextView highlightedTextView = null;
        //getArguments().getString(ACTIVE_MENU_ITEM);
        final Resources resources = Application.getAppResources();

        if (resources.getString(R.string.nav_option_news).equals(activeFragmentTitle)) {
            highlightedTextView = (TextView) getView().findViewById(R.id.nav_option_news);
            
        } else if (resources.getString(R.string.nav_option_markets).equals(activeFragmentTitle)) {
            highlightedTextView = (TextView) getView().findViewById(R.id.nav_option_markets);
            
        } else if (resources.getString(R.string.nav_option_lists).equals(activeFragmentTitle)) {
            highlightedTextView = (TextView) getView().findViewById(R.id.nav_option_lists);
        } else if (resources.getString(R.string.nav_option_alerts).equals(activeFragmentTitle)) {
            highlightedTextView = (TextView) getView().findViewById(R.id.nav_option_alerts);
        }
        else if (resources.getString(R.string.nav_option_briefcase).equals(activeFragmentTitle)) {
            highlightedTextView = (TextView) getView().findViewById(R.id.nav_option_briefcase);
        } else {
            highlightedTextView = (TextView) getView().findViewById(R.id.nav_option_dashboard);
        }

        highlightedTextView.setTextColor(getResources().getColor(R.color.dark_orange));

    }
对应的xml:

<?xml version="1.0" encoding="utf-8"?>
    
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:foo="http://schemas.android.com/apk/res/com.justin.a"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_texture"
    android:clickable="true" >
    
<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="@dimen/nav_margin"
    android:layout_marginTop="@dimen/nav_margin"
    android:layout_marginRight="@dimen/nav_margin"
    android:layout_marginBottom="@dimen/nav_margin"
    android:background="#242424"
    >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="@dimen/nav_padding"
        android:paddingTop="@dimen/nav_padding"
        android:paddingRight="@dimen/nav_padding"
        
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:divider="@android:drawable/divider_horizontal_bright"
            android:orientation="vertical"
            android:showDividers="middle"
                            
             >

         
             <com.justin.a.utils.FontTextView
                android:id="@+id/nav_option_dashboard"
                android:layout_width="match_parent"
                android:layout_height="@dimen/button_ht"
                android:layout_marginBottom="1px"
                android:onClick="onDashboardClicked"
                android:text="@string/nav_option_dashboard"
                android:textSize="@dimen/navigation_panel_text"
                foo:customFont="cabin.medium.ttf"
                android:padding="@dimen/nav_option_padding"
                android:background="@drawable/nav_background_button"
                    />  
            <com.justin.a.utils.FontTextView
                android:id="@+id/nav_option_news"
                android:layout_width="match_parent"
                android:layout_height="@dimen/button_ht"
                foo:customFont="cabin.medium.ttf"
                android:textSize="@dimen/navigation_panel_text"
                android:layout_marginBottom="1px"
                android:onClick="onNewsClicked"
                android:text="@string/nav_option_news"
                android:padding="@dimen/nav_option_padding"
                android:background="@drawable/nav_background_button"
                
                 />

            <com.justin.a.utils.FontTextView
                android:id="@+id/nav_option_markets"
                android:layout_width="match_parent"
                android:layout_height="@dimen/button_ht"
                android:textSize="@dimen/navigation_panel_text"
                android:layout_marginBottom="1px"
                android:onClick="onMarketClicked"
                android:text="@string/nav_option_markets" 
                foo:customFont="cabin.medium.ttf"
                android:padding="@dimen/nav_option_padding"
                android:background="@drawable/nav_background_button"

                />

            <com.justin.a.utils.FontTextView
                android:id="@+id/nav_option_lists"
                android:layout_width="match_parent"
                android:layout_height="@dimen/button_ht"
                android:textSize="@dimen/navigation_panel_text"
                android:layout_marginBottom="1px"
                android:text="@string/nav_option_lists"
                foo:customFont="cabin.medium.ttf"
                android:onClick="onListsClicked"
                android:padding="@dimen/nav_option_padding"
                android:background="@drawable/nav_background_button"

               />

            
            <com.justin.a.utils.FontTextView
                android:id="@+id/nav_option_briefcase"
                android:layout_width="match_parent"
                android:layout_height="@dimen/button_ht"
                android:textSize="@dimen/navigation_panel_text"
                android:layout_marginBottom="1px"
                foo:customFont="cabin.medium.ttf"
                android:padding="@dimen/nav_option_padding"
                android:onClick="onBriefcaseClicked"
                android:text="@string/nav_option_briefcase" 
                android:background="@drawable/nav_background_button"

                
                />

        </LinearLayout>
    </ScrollView>

</RelativeLayout>
</RelativeLayout>

为了消除混淆,简单地说,假设我打开导航抽屉,仪表板默认高亮显示(橙色高亮显示),然后我单击say markets,它打开市场屏幕,根据要求,我需要高亮显示市场按钮文本,而不是仪表板,在导航抽屉出现之前,我曾经使用过一个简单的导航面板,我曾经使用过这个面板
activeFragmentTitle=getArguments().getString(活动菜单项);(参考java代码,它现在在代码中被注释掉了),但在我添加nav drawer后它导致崩溃,因此我不得不注释掉它并使activeFragmenttile=“”;正如您从上面的java代码中看到的,如何将前一行代码放入代码中,这样就不会出现崩溃,从而达到所需的目标?

您是否已经尝试过扩展CheckedTextView而不是TextView

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/nav_option_something"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/selector"
android:gravity="center_vertical"
android:textColor="@drawable/selector_to_highlight_the_text"
android:textStyle="bold" />

这里是选择器,用于突出显示text.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="@color/white_text"/>
<item android:state_activated="true" android:color="@color/white_text"/>
<item android:state_pressed="true" android:color="@color/white_text"/>
<item android:state_enabled="false" android:color="@color/black_text"/>
<item android:color="@color/black_text"/>


只需在所有
com.justin.a.utils.FontTextView
中使用下面的选择器(
text\u selector.xml
)提及文本颜色属性即可

比如:

与:



我一直在关注FragmentStackManager和FragmentInfo,但我不知道它是什么……很难看到您提供的类和布局的进展情况。你能用截图来说明这个问题吗?嗨,伙计们,我添加了一个截图和更好的解释,请检查一下,你在这里调用highlightActiveMenuItem()?我看不到有人打电话给它。我在activitycreated()方法中调用了它,我在问题中添加了该块以供参考。我希望文本在做出新选择之前保持高亮显示,并且根据我的代码,必须有一种方法使这一行工作activeFragmentTitle=getArguments().getString(活动菜单项);没有任何崩溃,只是不知道如何
<com.justin.a.utils.FontTextView
 android:textColor="@drawable/text_selector"
<--other attributes-->
/>  
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_selected="true" android:color="@color/dark_orange"></item>
<item android:state_selected="false" android:color="@android:color/white"></item>

</selector>
 highlightedTextView.setTextColor(getResources().getColor(R.color.dark_orange));
highlightedTextView.setSelected(true);
<selector xmlns:android="http://schemas.android.com/apk/res/android">    
    <item android:state_pressed="true">        
          <shape 
   android:shape="rectangle" android:padding="7dp">

 <solid android:color="#F4886C"/> 
    <corners
     android:bottomRightRadius="7dp"
     android:bottomLeftRadius="7dp"
     android:topLeftRadius="7dp"
     android:topRightRadius="7dp"/>

 <stroke android:width="1px" android:color="#ffffff" />
</shape>  
    </item>    
    <item android:state_pressed="false">        
           <shape 
   android:shape="rectangle" android:padding="7dp">

 <solid android:color="#E43D12"/> 
    <corners
     android:bottomRightRadius="7dp"
     android:bottomLeftRadius="7dp"
     android:topLeftRadius="7dp"
     android:topRightRadius="7dp"/>

 <stroke android:width="1px" android:color="#ffffff" />
</shape>
     </item>

 </selector>