嗨,我一直在試圖弄清楚,但我不能。我有這個按鈕當屏幕尺寸減小時,如何使按鈕自動縮小?
<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文件?我是否刪除寬度?
所以把的CocE第一塊在java文件,並把第二個在XML佈局文件? – 2012-01-19 02:46:33
沒有它的所有java代碼,你初始化按鈕的地方就是你要調用'button.setWidth(pixels)'的地方。我編輯了我的回答 – tyczj 2012-01-19 07:33:58
編輯答案使用你的新的代碼。我確定如何讓編輯功能起作用?感謝您的持續幫助! – 2012-01-19 17:09:57