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

时间:2013-12-10 21:24:27

标签: android android-layout android-fragments android-linearlayout android-button

我的代码中有一个导航抽屉。导航抽屉有按钮,可以浏览app.Now当我点击按钮时只按下按钮Highlights。我期待的是按钮与文本一起突出显示,当我再次拉出导航抽屉时它会显示最后一个按钮文本突出显示,直到我点击另一个按钮突出显示点击并保持突出显示,直到做出另一个选择。这是我的代码:

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>

enter image description here 为了清除混乱,用简单的话说,假设我打开导航抽屉,默认情况下仪表板突出显示(橙色突出显示),然后我点击说市场,打开市场屏幕,按要求我需要市场按钮文字要突出显示而不是仪表板,但它不会做同样的事情并保持仪表板一直突出显示。导航抽屉之前,我曾经使用一个简单的导航面板,在那我曾经有 activeFragmentTitle = getArguments()。getString(ACTIVE_MENU_ITEM); (请参阅java代码,它现在在代码中注释掉了),但是当我添加导航抽屉后它导致崩溃,因此我不得不将它注释掉并使activeFragmenttile =“”;正如您从上面的java代码中看到的,如何将前一行代码放入代码中,以便没有崩溃并且达到了所需的目标?

3 个答案:

答案 0 :(得分:1)

您是否已经尝试过将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" />

这里是selector_to_highlight_the_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"/>

答案 1 :(得分:0)

只需在所有text_selector.xml中使用以下选择器(com.justin.a.utils.FontTextView)提及文字颜色属性。

像:

<com.justin.a.utils.FontTextView
 android:textColor="@drawable/text_selector"
<--other attributes-->
/>  

text_selector.xml:

<?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>

在代码中只需替换Line:

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

使用:

highlightedTextView.setSelected(true);

答案 2 :(得分:-1)

<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>