當前位置:首頁 » 安卓系統 » setmarginsandroid

setmarginsandroid

發布時間: 2024-06-02 09:53:30

1. android設置布局 大小

可以再代碼中定義listview的大小,動態設置顯示大小,根據解析度設置。

2. 有誰知道怎麼實現android界面整體縮放

android界面實現整體縮放
代碼:
float basevectorX = 0.0f
float basevectorY = 0.0f
//浮點坐標 左上角坐標
float FfrogX = 0.0f;
float FfrogY = 0.0f;
//整形坐標 左上角坐標
int frogX = 0;
int frogY = 0;

Rect rect = new Rect();
FfrogX = FfrogX+basevectorX;
frogX = (int)FfrogX;
FfrogY = FfrogY+basevectorY;
frogY = (int) FfrogY;
rect.left= frogX;
rect.right= ScreenWidth-rect.left-Width; //640
rect.top= frogY; //480
rect.bottom= ScreenHeight-rect.top-Height; //480
((MarginLayoutParams) frogview.getLayoutParams()).setMargins(rect.left, rect.top, rect.right, rect.bottom);
//這個可以放 大縮小,和移動。
CenterX = rect.left + Width/2; //中心點X坐標 用來判斷的 用於2d
CenterY = rect.top + Height/2; //中心點Y坐標 用來判斷的 用於2d

3. android 鐐瑰嚮鎸夐挳瀹炵幇鎸夐挳鍥劇墖浠1鍘樼背鍙崇Щ

涓嶅仠鍦板埛鏂伴噸鏂扮粯鍒舵寜閽灝辮屼簡銆
ImageButton myButton1錛宮yButton2;//鎸夐挳
RelativeLayout.LayoutParams layoutParam;//璁劇疆鎸夐挳浣嶇疆
ReletiveLayout layoutGame;//瀹瑰櫒錛屾斁緗鎸夐挳鐨勪綅緗
myButton1 = new ImageButton(myActivity.this);
myButton1 .setBackgroundResource(R.drawable.pleaseclick);
layoutParam = new RelativeLayout.LayoutParams(120,80);//璁劇疆鎸夐挳澶у皬
layoutParam.setMargins(70, 65, 610, 335);//璁劇疆鎸夐挳鐨勪綅緗
layoutGame.addView(imgHand, layoutParam);

浠ヤ笂鏄絎涓嬈℃坊鍔犳寜閽鐨勬椂鍊欑殑浠g爜錛屼箣鍚庢瘡嬈$偣鍑繪寜閽2鐨勬椂鍊欏彲浠ユ墽琛
layoutParam.setMargins(620, 90, 60, 310);
layoutGame.updateViewLayout(myButton1, layoutParam);
閫氳繃璁劇疆閲岄潰鐨勬暟鍊肩殑鍙樺寲鏉ュ彉鎹浣嶇疆銆

4. android開發 如何在相對布局中動態添加控制項

首先setMargin方法不是RelativeLayout的方法,而是RelativeLayout.LayoutParams的方法。
你應該這麼用:
RelativeLayout layout = new RelativeLayout(this);
layout.setLayoutParams(new RelativeLayout.LayoutParams(-1, -1));
TextView mView = new TextView(this);
mView.setId(2);
mView.setText("this is a test text!");
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
-2, -2);
// layoutParams.setMargins(100, 100, 100, 100);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, -1);
layout.addView(mView, layoutParams);
上例是將一個TextView添加到RelativeLayout的底部。你可以把注釋行取消掉,把下一行注釋,再看下效果。

5. Android如何在java代碼中設置margin

1、比如imageView,有一個getLayout方法,獲得的layout在強轉類型到LinearLayout或者其他,然後再設定margin什麼的。

2、我們平常可以直接在xml里設置margin,如:

Xml代碼 <ImageViewandroid:layout_margin="5dip"android:src="@drawable/image"/>

但是有些情況下,需要在java代碼里來寫,可是View本身沒有setMargin方法,怎麼辦呢?

通過查閱android api,我們發現android.view.ViewGroup.MarginLayoutParams有個方法

setMargins(left, top, right, bottom)。

其直接的子類有: FrameLayout.LayoutParams, LinearLayout.LayoutParams and RelativeLayout.LayoutParams。

    6. 安卓編程 如何在Java代碼里設置button的margin(外邊距)

    1、獲取按鈕的LayoutParams

    LinearLayout.LayoutParamslayoutParams=(LinearLayout.LayoutParams)button.getLayoutParams();

    2、在LayoutParams中設置margin

    layoutParams.setMargins(100,20,10,5);//4個參數按順序分別是左上右下

    3、把這純仔掘戚輪個LayoutParams設置給做核按鈕

    button.setLayoutParams(layoutParams);//mView是控制項

    7. android動態添加控制項,怎樣指定位置

    首先你得定義一個 LayoutParams:
    RelativeLayout.LayoutParams s = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
    s.addRule(RelativeLayout.CENTER_IN_PARENT, -1);
    //添加位置信息 -1表示相對於父控制項的位置 ,如果要相對某個平級控制項則參數是該控制項的ID

    s.setMargins(10, 10, 10, 10);//設置左,上,右,下,的距離

    上面的定義好了之後可以用了:

    imgApple2.setLayoutParams(s);
    insertLayout.addView(imgApple2,100,100);

    熱點內容
    手機百度雲如何秒上傳 發布:2025-07-17 11:46:27 瀏覽:612
    交易貓買腳本靠譜嗎 發布:2025-07-17 11:32:27 瀏覽:520
    伺服器系統盤一般做什麼陣列 發布:2025-07-17 11:16:05 瀏覽:363
    如何用c語音實現放射密碼 發布:2025-07-17 11:13:03 瀏覽:735
    lol腳本2017 發布:2025-07-17 10:52:18 瀏覽:827
    編譯條件有哪些 發布:2025-07-17 10:42:51 瀏覽:334
    apache設置靜態內容緩存時間 發布:2025-07-17 10:32:32 瀏覽:295
    蘋果6不能緩存的視頻 發布:2025-07-17 10:31:44 瀏覽:405
    如何把文件夾共享 發布:2025-07-17 10:23:49 瀏覽:299
    linux查看命令大全 發布:2025-07-17 10:21:24 瀏覽:976