Bạn có thể tạo bộ điều khiển chính để xử lý tất cả các bộ tỷ lệ của mình.
Ví dụ, bạn có một bố cục:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/my_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:textSize="16sp" />
</RelativeLayout>
Và bạn quy mô muốn văn bản kích thước android:textSize="16sp"
bởi một quy mô dimens.xml
.
Tạo tỷ lệ quy mô của bạn trong một dimens.xml
:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="scaleRate" format="float" type="dimen">2.5</item>
</resources>
Và sau khi bạn tạo bạn điều khiển chính như thế này:
package com.example.stackoverflowsandbox;
import android.content.Context;
import android.widget.TextView;
public class MyScaleController {
public static void applyTextSizeScale(final Context context, final TextView myTextView) {
final float currentTextSize = myTextView.getTextSize();
myTextView.setTextSize(currentTextSize * context.getResources().getDimension(R.dimen.scaleRate));
}
}
Vì vậy, bạn sử dụng như thế:
package com.example.stackoverflowsandbox;
import android.app.Activity;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(final android.os.Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_main);
final TextView myTextView = (TextView) this.findViewById(R.id.my_text_view);
MyScaleController.applyTextSizeScale(this, myTextView);
}
}
Bây giờ bạn có thể tạo nhiều kích thước tỷ lệ nhỏ cho nhiều kích thước màn hình. Những gì bạn wann ở lần đầu tiên, làm cho calc toán học trong tập tin XML, sẽ không hoạt động. Nhưng theo cách tôi đoán bạn có cách tiếp cận tốt nhất.
Nguồn
2014-07-15 14:03:26
Tôi đoán không, nhưng bạn có thể đặt động cơ mờ, ví dụ: kích thước văn bản trong một TextView. –
Ý của bạn là Marcelo? Bạn có thể giải thích về nó trong câu trả lời không? – neoswf
Mục đích cuối cùng của bạn là gì trong việc kiểm tra kích thước phông chữ như thế này? Bạn đang thử nghiệm cho nhiều màn hình? –