简体   繁体   English

如何同时更改多个XML属性

[英]How to change multiple XML Attributes at the same time

I was wondering how to change the same XML Attribute (like android:textSize to 19sp ) for multiple TextViews in android studio. 我想知道如何在android studio中为多个TextViews更改相同的XML属性(例如android:textSize19sp )。 I saw this question on setting global styles , but I don't want to change all of the TextViews , just most of them... 在设置全局样式时看到了这个问题 ,但是我不想更改所有TextViews ,只是大多数都可以...

Is there any way this can be done? 有什么办法可以做到吗? Thanks! 谢谢!

If you are looking forward to changing your text size in a more automated way only inside some TextViews (not all) you should stop hardcoding stuff and doing it more programmatically. 如果您希望仅在某些TextView(不是全部)内以更自动化的方式更改文本大小,则应停止对内容进行硬编码并以编程方式进行。 You provided little information about your project but for example, if you have an activity with 5 TextViews the best way to set a single text size through only some of them is though IDs. 您提供的有关项目的信息很少,但是例如,如果您有一个包含5个TextViews的活动,则通过ID设置仅通过其中一些设置单个文本大小的最佳方法。 You need to set a different ID to every TextView (like t0, t1, t2, t3, t4). 您需要为每个TextView设置不同的ID(例如t0,t1,t2,t3,t4)。 Here is how your TextViews would look like: 这是您的TextViews的外观:

<TextView
    android:id="@+id/t0"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true"
    android:layout_marginBottom="297dp"
    android:layout_marginStart="139dp"
    android:text="wqertyu" />

<TextView
    android:id="@+id/t1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginEnd="-115dp"
    android:layout_marginTop="304dp"
    android:text="2134253647" />

<TextView
    android:id="@+id/t2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginStart="115dp"
    android:layout_marginTop="95dp"
    android:text="gsdse5467y" />

<TextView
    android:id="@+id/t3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="-242dp"
    android:layout_marginEnd="-139dp"
    android:text="AAAAAAAAAAA" />

<TextView
    android:id="@+id/t4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentTop="true"
    android:layout_marginEnd="101dp"
    android:layout_marginTop="242dp"
    android:text="32453645786" />

If you want that only your t2,t3 and t4 to have the same text size, you can do it into your java file like this: 如果只希望t2,t3和t4具有相同的文本大小,则可以将其放入Java文件中,如下所示:

public class activity_test extends AppCompatActivity {

TextView t0,t1,t2,t3,t4;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);

    t2=findViewById(R.id.t2);
    t2.setTextSize(19);
    t3=findViewById(R.id.t3);
    t3.setTextSize(19);
    t4=findViewById(R.id.t4);
    t4.setTextSize(19);

}

} }

You can basically do the same thing you do in your XML file as well in java. 基本上,您可以像在XML文件中一样执行相同的操作。 Hope this helped. 希望这会有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 相同xml文件的多个属性 - Multiple attributes of the same xml file 如何在 XML 模式中指定不能同时存在 2 个属性? - How to specify in a XML schema that 2 attributes can't be present at the same time? 在同一XML中查询多个XML属性 - Querying for multiple XML attributes in the same XML 如何对具有相同名称的多个属性的XML API响应进行脱盐处理 - How to Desalinize XML API Response with Multiple Attributes with same name 在Python中解析XML:多个相同的属性 - Parsing XML in Python: Multiple same attributes 如何用Jdom同时更改读写XML的输出编码? - How to change output coding of a XML of reading and writing at the same time in with Jdom? Android Studio:如何同时影响多个视图? (XML + 科特林) - Android Studio: How to affect multiple Views at the same time? (XML + Kotlin) 如何将带有属性和内部文本的XML标记同时转换为PHP中的SimpleXMLElement对象? - How do I convert XML tags with attributes and inner text at the same time into a SimpleXMLElement object in PHP? 如何使用 php-soap 同时获取具有属性和文本内容的 xml-node 作为子节点? - How to get xml-node with attributes and text content as child at same time with php-soap? 当我们有多个具有相同名称但属性不同的元素时,如何使用Xdocument从xml中删除元素 - How to remove an element from an xml using Xdocument when we have multiple elements with same name but different attributes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM