简体   繁体   中英

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. I saw this question on setting global styles , but I don't want to change all of the TextViews , just most of them...

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. 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. You need to set a different ID to every TextView (like t0, t1, t2, t3, t4). Here is how your TextViews would look like:

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

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. Hope this helped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM