How to make buttons automatically scale down, when screen size decrease?

回眸只為那壹抹淺笑 提交于 2020-01-03 05:32:24

问题


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!

EDIT: 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);
       }
    });

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


回答1:


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

then when you create the button use

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

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




回答2:


In addition to @tyczj answer you may call:

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


来源:https://stackoverflow.com/questions/8916363/how-to-make-buttons-automatically-scale-down-when-screen-size-decrease

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!