【问题标题】:How do I highlight text with button highlights?如何使用按钮突出显示文本?
【发布时间】:2013-12-10 21:24:26
【问题描述】:

我的代码中有一个导航抽屉。导航抽屉有允许浏览应用程序的按钮。现在,当我单击按钮时,只有按钮突出显示。我期望按钮与文本一起突出显示,当我再次拉出导航抽屉时,它会显示最后一个按钮文本突出显示,直到我单击另一个按钮,该按钮在单击时突出显示并保持突出显示,直到做出另一个选择。这是我的相同代码:

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>

为了消除混淆,简单来说,假设我打开导航抽屉,仪表板默认突出显示(橙色突出显示),然后我点击说市场,它打开市场屏幕,根据要求我需要市场按钮文本要突出显示而不是仪表板,但它不会做同样的事情并始终保持仪表板突出显示。在导航抽屉之前,我曾经使用一个简单的导航面板,因为我曾经有 activeFragmentTitle =getArguments().getString(ACTIVE_MENU_ITEM); (参考java代码,现在在代码中注释掉了),但是在我添加导航抽屉后它导致崩溃,因此我不得不将它注释掉并制作activeFragmenttile =“”;正如您从上面的 java 代码中看到的那样,如何将前一行代码放入代码中,这样就不会发生崩溃并达到所需的目标?

【问题讨论】:

  • 我一直在寻找 FragmentStackManager 和 FragmentInfo,但我不知道它是什么......
  • 很难看到您提供的课程和布局发生了什么。你能用截图说明问题吗?
  • 大家好,我添加了一个截图和更好的截图解释,请查看
  • 在哪里调用 highlightActiveMenuItem()?我没有看到它在任何地方被调用。请同时发布该部分代码,以便我们进行故障排除。
  • 我在 onActivityCreated() 方法中调用它,我在问题中添加了该块以供参考

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


【解决方案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"/>

【讨论】:

  • 我希望文本保持突出显示,直到做出新的选择,并且基于我的代码
  • 必须有办法让这条线工作 activeFragmentTitle =getArguments().getString(ACTIVE_MENU_ITEM);没有任何崩溃,只是不知道如何
【解决方案2】:

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

喜欢:

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

在代码中只需替换行:

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

与:

highlightedTextView.setSelected(true);

【讨论】:

    【解决方案3】:
    <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> 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-27
      • 1970-01-01
      • 2017-02-04
      相关资源
      最近更新 更多