2012-01-18 103 views
0

嗨,我一直在试图弄清楚,但我不能。我有这个按钮当屏幕尺寸减小时,如何使按钮自动缩小?

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

回答

2

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

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以像素为单位设置宽度,因此您可能必须对大小进行一些调整以获得所需的不同屏幕尺寸

+0

所以把的CocE第一块在java文件,并把第二个在XML布局文件? – 2012-01-19 02:46:33

+0

没有它的所有java代码,你初始化按钮的地方就是你要调用'button.setWidth(pixels)'的地方。我编辑了我的回答 – tyczj 2012-01-19 07:33:58

+0

编辑答案使用你的新的代码。我确定如何让编辑功能起作用?感谢您的持续帮助! – 2012-01-19 17:09:57

0

除了@tyczj回答您可致电:

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