【问题标题】:How to make buttons automatically scale down, when screen size decrease?当屏幕尺寸减小时,如何使按钮自动缩小?
【发布时间】:2012-01-20 01:09:13
【问题描述】:

您好,我一直在努力解决这个问题,但我做不到。我有这个按钮

<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" 
>

当这个按钮出现在较小的屏幕上时,我怎样才能使它的按钮也缩小自己的尺寸?谢谢!

编辑:好的,这是新代码。但是我不确定如何使编辑功能起作用?我有进口的东西吗?

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 文件?我要删除宽度吗?

【问题讨论】:

    标签: android button scale


    【解决方案1】:

    由于您将其设置为硬宽度,因此您必须执行类似的操作

    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;
            }
    }
    

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

    然后当你创建按钮使用

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

    setWidth 以像素为单位设置宽度,因此您可能需要调整尺寸才能在不同的屏幕尺寸上获得所需的内容

    【讨论】:

    • 那么把第一块coce放到java文件里,把第二块放到xml布局文件里?
    • 不是所有的 java 代码,你初始化按钮的地方就是你调用button.setWidth(pixels) 的地方。我编辑了我的答案
    • 已编辑答案以使用您的显示新代码。我确定如何使编辑功能起作用?感谢您一直以来的帮助!
    • edit 不是您需要的东西,重要的是获得您拥有的显示器。使用您需要的编辑SharedPreferences.Editor edit = pref.edit();
    • 非常感谢您到目前为止帮助我。只需要更好地理解这一点。按钮的宽度由开关盒决定。那我该如何使用button.setWidth?现在我把它设置成这样 goHereButton.setWidth(20);我没有看到任何变化。这个java代码如何覆盖xml布局文件中的硬编码宽度?再次感谢!
    【解决方案2】:

    除了@tyczj 回答你还可以打电话:

    Button button = (Button)findViewById(R.id.your_button_id);
    button.setLayoutParams(new LayoutParams(pixels, LayoutParams.WRAP_CONTENT));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-19
      • 2013-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多