簡體   English   中英

如何同時更改多個XML屬性

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

我想知道如何在android studio中為多個TextViews更改相同的XML屬性(例如android:textSize19sp )。 在設置全局樣式時看到了這個問題 ,但是我不想更改所有TextViews ,只是大多數都可以...

有什么辦法可以做到嗎? 謝謝!

如果您希望僅在某些TextView(不是全部)內以更自動化的方式更改文本大小,則應停止對內容進行硬編碼並以編程方式進行。 您提供的有關項目的信息很少,但是例如,如果您有一個包含5個TextViews的活動,則通過ID設置僅通過其中一些設置單個文本大小的最佳方法。 您需要為每個TextView設置不同的ID(例如t0,t1,t2,t3,t4)。 這是您的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" />

如果只希望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);

}

}

基本上,您可以像在XML文件中一樣執行相同的操作。 希望這會有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM