Android layout 如何将textview大小更改为android中的所有设备

Android layout 如何将textview大小更改为android中的所有设备,android-layout,android-emulator,Android Layout,Android Emulator,您好,我正在android上为所有大小的手机和平板电脑开发一个应用程序。图像视图显示的大小都很好。但我在textview字体大小方面遇到了问题。在我的应用程序中,我需要显示具有该背景和文本的textview,但不同大小的文本大小显示不正确。有想法的人请帮助我。我试着使用下面的代码 MainActivity.class public class MainActivity extends Activity { float screenHeight,screenWidth,screendensit

您好,我正在android上为所有大小的手机和平板电脑开发一个应用程序。图像视图显示的大小都很好。但我在textview字体大小方面遇到了问题。在我的应用程序中,我需要显示具有该背景和文本的textview,但不同大小的文本大小显示不正确。有想法的人请帮助我。我试着使用下面的代码

MainActivity.class

 public class MainActivity extends Activity {
 float screenHeight,screenWidth,screendensity;
 RelativeLayout alpha_page2;
 ImageView alpha_back,alpha_back1;
 TextView option121;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
     screenHeight = displaymetrics.heightPixels;
     screenWidth = displaymetrics.widthPixels;
     screendensity = displaymetrics.densityDpi;
     Log.i("screenHeight",""+screenHeight);
     Log.i("screenWidth",""+screenWidth);
     Log.i("screendensity",""+screendensity);
    setContentView(R.layout.activity_main);

    int letpading=(int)(116*(screenWidth/1024));
    int toppading=(int)(79*(screenHeight/600));

    int textsiz=(int)(50*(screendensity/600));
    option121 = (TextView)findViewById(R.id.text1);
    option121.setBackgroundResource(R.drawable.dog_b_blank);
    option121.setText("A");
    option121.setText(Color.BLACK);
    RelativeLayout.LayoutParams layoutoption121 = (RelativeLayout.LayoutParams) option121.getLayoutParams();       
    layoutoption121.height=(int)(180*(screenHeight/600));  
    layoutoption121.width=(int)(180*(screenWidth/1024));
    layoutoption121.topMargin=(int)(100*(screenHeight/600));
    layoutoption121.leftMargin= (int)(250*(screenWidth/1024));
    option121.setPadding(letpading, toppading, 0, 0);

    option121.setTextSize(textsiz);
    }

}

不是这样做的。Android为您提供文本大小,在指定文本大小时使用“sp”(缩放独立像素)单位,如果您需要为不同屏幕配置不同大小,请阅读以下内容:

基本上,您需要在适当的资源目录中的.xml文件中定义文本大小:

res/values大 res/values xlarge 等等


然后在布局xml或代码中引用这些常量。

thanku。但如果我这样做,这意味着在所有大小中都可以正常工作,我将在文本大小中面对prblm。在xml中,我将给出android:textSize=“50sp”表示一个大小正常工作。对于另一个大小,请阅读链接。听起来您想为不同的屏幕大小指定不同的文本大小,对吗?这个链接描述了如何做到这一点。我也很喜欢这个,但我确实得到了你。你有其他的解决方案吗?你可以告诉我,这就是方法。如果您需要更多帮助,请发布您尝试的内容。您不应该试图在运行时根据SCENSIZE计算文本大小。这是不必要的,而且非常丑陋。你需要阅读并理解我提供的链接。在特定于屏幕大小的.xml文件中定义文本大小,并在代码或xml布局中使用这些常量。Android将自动为您的屏幕大小选择正确的资源文件。