How to programmatically set style attribute in a view

前端 未结 11 2028
北恋
北恋 2020-11-22 06:38

I\'m getting a view from the XML with the code below:

Button view = (Button) LayoutInflater.from(this).inflate(R.layout.section_button, null);
相关标签:
11条回答
  • 2020-11-22 06:41

    Generally you can't change styles programmatically; you can set the look of a screen, or part of a layout, or individual button in your XML layout using themes or styles. Themes can, however, be applied programmatically.

    There is also such a thing as a StateListDrawable which lets you define different drawables for each state the your Button can be in, whether focused, selected, pressed, disabled and so on.

    For example, to get your button to change colour when it's pressed, you could define an XML file called res/drawable/my_button.xml directory like this:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item
        android:state_pressed="true"
        android:drawable="@drawable/btn_pressed" />
      <item
        android:state_pressed="false"
        android:drawable="@drawable/btn_normal" />
    </selector>
    

    You can then apply this selector to a Button by setting the property android:background="@drawable/my_button".

    0 讨论(0)
  • 2020-11-22 06:44

    Yes, you can use for example in a button

    Button b = new Button(this);
    b.setBackgroundResource(R.drawable.selector_test);
    
    0 讨论(0)
  • 2020-11-22 06:49

    First of all, you don't need to use a layout inflater to create a simple Button. You can just use:

    button = new Button(context);
    

    If you want to style the button you have 2 choices: the simplest one is to just specify all the elements in code, like many of the other answers suggest:

    button.setTextColor(Color.RED);
    button.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
    

    The other option is to define the style in XML, and apply it to the button. In the general case, you can use a ContextThemeWrapper for this:

    ContextThemeWrapper newContext = new ContextThemeWrapper(baseContext, R.style.MyStyle);
    button = new Button(newContext);
    

    To change the text-related attributes on a TextView (or its subclasses like Button) there is a special method:

    button.setTextAppearance(context, R.style.MyTextStyle);
    

    This last one cannot be used to change all attributes; for example to change padding you need to use a ContextThemeWrapper. But for text color, size, etc. you can use setTextAppearance.

    0 讨论(0)
  • 2020-11-22 06:52

    If you are using the Support library, you could simply use

    TextViewCompat.setTextAppearance(textView, R.style.AppTheme_TextStyle_ButtonDefault_Whatever);
    

    for TextViews and Buttons. There are similar classes for the rest of Views :-)

    0 讨论(0)
  • 2020-11-22 06:53

    At runtime, you know what style you want your button to have. So beforehand, in xml in the layout folder, you can have all ready to go buttons with the styles you need. So in the layout folder, you might have a file named: button_style_1.xml. The contents of that file might look like:

    <?xml version="1.0" encoding="utf-8"?>
    <Button
        android:id="@+id/styleOneButton"
        style="@style/FirstStyle" />
    

    If you are working with fragments, then in onCreateView you inflate that button, like:

    Button firstStyleBtn = (Button) inflater.inflate(R.layout.button_style_1, container, false);
    

    where container is the ViewGroup container associated with the onCreateView method you override when creating your fragment.

    Need two more such buttons? You create them like this:

    Button secondFirstStyleBtn = (Button) inflater.inflate(R.layout.button_style_1, container, false);
    Button thirdFirstStyleBtn = (Button) inflater.inflate(R.layout.button_style_1, container, false);
    

    You can customize those buttons:

    secondFirstStyleBtn.setText("My Second");
    thirdFirstStyleBtn.setText("My Third");
    

    Then you add your customized, stylized buttons to the layout container you also inflated in the onCreateView method:

    _stylizedButtonsContainer = (LinearLayout) rootView.findViewById(R.id.stylizedButtonsContainer);
    
    _stylizedButtonsContainer.addView(firstStyleBtn);
    _stylizedButtonsContainer.addView(secondFirstStyleBtn);
    _stylizedButtonsContainer.addView(thirdFirstStyleBtn);
    

    And that's how you can dynamically work with stylized buttons.

    0 讨论(0)
  • 2020-11-22 06:54

    You can do style attributes like so:

    Button myButton = new Button(this, null,android.R.attr.buttonBarButtonStyle);
    

    in place of:

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn"
        style="?android:attr/buttonBarButtonStyle"
    
        />
    
    0 讨论(0)
提交回复
热议问题