如何同时更改多个 XML 属性
How to change multiple XML Attributes at the same time
我想知道如何在 android 工作室中为多个 TextViews
更改相同的 XML 属性(例如 android:textSize
到 19sp
)。我看到了this question on setting global styles,但是我不想改变TextViews
的全部,只是大部分...
有什么办法可以做到吗?谢谢!
如果您希望仅在某些 TextView(不是全部)内以更自动化的方式更改文本大小,则应停止硬编码,更多地以编程方式进行。您提供的项目信息很少,但是例如,如果您有一个带有 5 个 TextView 的 activity,则仅通过其中一些设置单个文本大小的最佳方法是通过 ID。您需要为每个 TextView 设置不同的 ID(如 t0、t1、t2、t3、t4)。这是您的 TextView 的样子:
<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 文件中以及在 java 中执行相同的操作。希望这对您有所帮助。
我想知道如何在 android 工作室中为多个 TextViews
更改相同的 XML 属性(例如 android:textSize
到 19sp
)。我看到了this question on setting global styles,但是我不想改变TextViews
的全部,只是大部分...
有什么办法可以做到吗?谢谢!
如果您希望仅在某些 TextView(不是全部)内以更自动化的方式更改文本大小,则应停止硬编码,更多地以编程方式进行。您提供的项目信息很少,但是例如,如果您有一个带有 5 个 TextView 的 activity,则仅通过其中一些设置单个文本大小的最佳方法是通过 ID。您需要为每个 TextView 设置不同的 ID(如 t0、t1、t2、t3、t4)。这是您的 TextView 的样子:
<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 文件中以及在 java 中执行相同的操作。希望这对您有所帮助。