如何使按钮自动缩小,当屏幕尺寸减小?按钮、屏幕尺寸

2023-09-08 08:57:22 作者:誓言再美也不过是谎言

您好我一直在试着想出解决办法,但我不能。我有这个按钮

Hi I've been tryin to figure this out but I can't. I have this button

<Button android:layout_alignLeft="@+id/button1" android:layout_below="@+id/button2"                
android:id="@+id/button3" 
android:layout_width="235dp" 
android:text="@string/spire"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content" 
>

在此按钮较小的屏幕我怎样才能使它的按键规模也较小自己上出现?谢谢!

When this button appears on smaller screen how can i make it the button also scale own smaller? Thanks!

编辑:好了,所以继承人新的code。但我不确定如何使编辑功能的工作?我一定进口的东西吗?

Okay so heres the new code. But I'm unsure of how to make the edit function work? do i have import something?

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.DisplayMetrics;
import android.view.View;
import android.content.Intent;
import android.content.SharedPreferences;
import android.widget.Button;
public void getDisplay(){
    SharedPreferences pref =  PreferenceManager.getDefaultSharedPreferences(this);
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
        switch(metrics.densityDpi){
        case DisplayMetrics.DENSITY_LOW:
            edit.putInt("SIZE", 12).commit();
            edit.putInt("POSITION", -8).commit();
            break;
        case DisplayMetrics.DENSITY_MEDIUM:
            edit.putInt("SIZE", 16).commit();
            edit.putInt("POSITION", -6).commit();
            break;
        case DisplayMetrics.DENSITY_HIGH:
            edit.putInt("SIZE", 25).commit();
            edit.putInt("POSITION", 8).commit();
            break;
        case DisplayMetrics.DENSITY_XHIGH:
            //edit.putInt("SIZE",35).commit();
            //edit.putInt("POSITION",30);
            break;
        }
}

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    goHereButton  = (Button) findViewById(R.id.button1);
    goHereButton.setWidth(20);
    goHereButton.setOnClickListener(new View.OnClickListener() 
    {
       public void onClick(View arg0) 
       {
       Intent i = new Intent(UMassGuide.this, DirectionsScreenActivity.class);
       startActivity(i);
       }
    });

另外我该怎么办XML文件?如何删除宽度?

Also what do i do the xml file? do i delete the width?

推荐答案

因为你将它设置一个硬的宽度,你会做这样的事情。

since you are setting it a hard width you would have to do something like this

public void getDisplay(){
    SharedPreferences pref =  PreferenceManager.getDefaultSharedPreferences(this);
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
        switch(metrics.densityDpi){
        case DisplayMetrics.DENSITY_LOW:
            edit.putInt("SIZE", 12).commit();
            edit.putInt("POSITION", -8).commit();
            break;
        case DisplayMetrics.DENSITY_MEDIUM:
            edit.putInt("SIZE", 16).commit();
            edit.putInt("POSITION", -6).commit();
            break;
        case DisplayMetrics.DENSITY_HIGH:
            edit.putInt("SIZE", 25).commit();
            edit.putInt("POSITION", 8).commit();
            break;
        case DisplayMetrics.DENSITY_XHIGH:
            //edit.putInt("SIZE",35).commit();
            //edit.putInt("POSITION",30);
            break;
        }
}

根据屏幕显示上创建一个宽度尺寸

based on the screen display create a width size

那么当您创建按钮使用

Button button = (Button)findViewById(R.id.your_button_id);
button.setWidth(pixels)

setWidth 设置由像素宽度,所以你可能需要做一些与大小乱搞,让你想在不同的屏幕尺寸是什么

setWidth sets the width by pixels so you might have to do some messing around with sizes to get what you want on different screen sizes