導航:首頁 > 操作系統 > android設置view居中

android設置view居中

發布時間:2022-10-05 09:54:49

A. android界面設計,這四個Textview怎麼設置垂直居中,,,,

外麵包裹一個相對布局,應該就可以了,再去設置這個相對布局在父布局中的位置

B. android設置textView水平居中顯示

首先不太明白你的問題啊
你到底想讓textView裡面的內容水平居中,還是讓textView控制項在它的父布局裡水平居中呢?
1.讓textView裡面的內容水平居中:android:gravity="center_horizontal"
2.讓textView控制項在它的父布局裡水平居中android:layout_gravity="center_horizontal"

C. 安卓的textview怎麼居中

android編程中textview居中的方法有2中方式

【主要方式】

  1. layout下的布局文件內居中。

  2. 代碼動態設置textview居中。



【主要原理】

調用android sdk提供的api方法進行設置textview 居中顯示。

【詳細實現方式】

  1. layou下的布局居中

    如下圖所示:

    java">TextView.setGravity(Gravity.CENTER);設置文字內容居中
    //設置控制項布局居中
    RelativeLayout.LayoutParamslayoutParams=newRelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);mTextView.setLayoutParams(layoutParams);

【最後】

推薦使用在layout下面進行設置,容易進行開發,調試。

D. android怎樣使自定義的view里的bitmap圖片居中

幾個重要知識點:

1、布局文件中android:scaleType="matrix" 設置圖片動態縮放

2、matrix.postTranslate(dx,dy); 平移圖片 matrix.postScale(sx,sy,p.x,p.y); 縮放圖片

3、兩點的中點、距離計算方式,限制縮放范圍函數,在指定區域內移動圖片方法

示例


{
privatestaticfinalintNONE=0;
privatestaticfinalintDRAG=1;
privatestaticfinalintZOOM=2;

privateintmode=NONE;
privatefloatoldDist;
privateMatrixmatrix=newMatrix();
privateMatrixsavedMatrix=newMatrix();
privatePointFstart=newPointF();
privatePointFmid=newPointF();

ImageViewview;
Bitmapbitmap;
DisplayMetricsdm;
floatminScaleR=0.1f;//最少縮放比例
staticfinalfloatMAX_SCALE=4f;//最大縮放比例
floatdist=1f;

@Override
publicvoidonCreate(BundlesavedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
view=(ImageView)findViewById(R.id.image_view);
bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.test);

dm=newDisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);//獲取解析度


matrix.setScale(minScaleR,minScaleR);//開始先縮小
matrix.postTranslate(120,120);//圖片的位置相對於imageview的左上角往右往下各偏移120個像素
view.setImageMatrix(matrix);

view.setOnTouchListener(newOnTouchListener()
{
@Override
publicbooleanonTouch(Viewv,MotionEventevent)
{
ImageViewview=(ImageView)v;
switch(event.getAction()&MotionEvent.ACTION_MASK)
{
caseMotionEvent.ACTION_DOWN://單點
Toast.makeText(Main.this,bitmap.getWidth()+"*"+bitmap.getHeight(),Toast.LENGTH_LONG).show();
savedMatrix.set(matrix);
start.set(event.getX(),event.getY());
mode=DRAG;
break;
caseMotionEvent.ACTION_UP://單點彈起
caseMotionEvent.ACTION_POINTER_UP://多點彈起
mode=NONE;
break;
caseMotionEvent.ACTION_POINTER_DOWN://多點
oldDist=spacing(event);
if(oldDist>10f)
{
savedMatrix.set(matrix);
midPoint(mid,event);
mode=ZOOM;
}
break;
caseMotionEvent.ACTION_MOVE://按下且在拖動
if(mode==DRAG)
{
matrix.set(savedMatrix);
matrix.postTranslate(event.getX()-start.x,event.getY()-start.y);//xy方向都可以拖動
//matrix.postTranslate(event.getX()-start.x,0);//只在x軸方向拖動即左右拖動上下不動
//matrix.postTranslate(0,event.getY()-start.y);//只在y軸方向拖動即上下拖動左右不動
}
elseif(mode==ZOOM)
{
floatnewDist=spacing(event);
if(newDist>10f)
{
matrix.set(savedMatrix);
floatscale=newDist/oldDist;
matrix.postScale(scale,scale,mid.x,mid.y);
}
}
break;
}

view.setImageMatrix(matrix);

CheckScale();//限制縮放范圍
center();//居中控制

returntrue;
}


//兩點的距離
privatefloatspacing(MotionEventevent)
{
floatx=event.getX(0)-event.getX(1);
floaty=event.getY(0)-event.getY(1);
returnFloatMath.sqrt(x*x+y*y);
}

//兩點的中點
privatevoidmidPoint(PointFpoint,MotionEventevent)
{
floatx=event.getX(0)+event.getX(1);
floaty=event.getY(0)+event.getY(1);
point.set(x/2,y/2);
}
});
}

//限制最大最小縮放比例
protectedvoidCheckScale()
{
floatp[]=newfloat[9];
matrix.getValues(p);
if(mode==ZOOM)
{
if(p[0]<minScaleR)
{
matrix.setScale(minScaleR,minScaleR);
}
if(p[0]>MAX_SCALE)
{
matrix.set(savedMatrix);
}
}
}

//自動居中左右及上下都居中
protectedvoidcenter()
{
center(true,true);
}

privatevoidcenter(booleanhorizontal,booleanvertical)
{
Matrixm=newMatrix();
m.set(matrix);
RectFrect=newRectF(0,0,bitmap.getWidth(),bitmap.getHeight());
m.mapRect(rect);
floatheight=rect.height();
floatwidth=rect.width();
floatdeltaX=0,deltaY=0;
if(vertical)
{
//intscreenHeight=dm.heightPixels;//手機屏幕解析度的高度
intscreenHeight=400;
if(height<screenHeight)
{
deltaY=(screenHeight-height)/2-rect.top;
}elseif(rect.top>0)
{
deltaY=-rect.top;
}elseif(rect.bottom<screenHeight)
{
deltaY=view.getHeight()-rect.bottom;
}
}

if(horizontal)
{
//intscreenWidth=dm.widthPixels;//手機屏幕解析度的寬度
intscreenWidth=400;
if(width<screenWidth)
{
deltaX=(screenWidth-width)/2-rect.left;
}elseif(rect.left>0)
{
deltaX=-rect.left;
}elseif(rect.right<screenWidth)
{
deltaX=screenWidth-rect.right;
}
}
matrix.postTranslate(deltaX,deltaY);
}
}
[html]viewplain
<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:layout_width="200dip"
android:layout_height="200dip"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:scaleType="matrix"
android:src="@drawable/test"
android:id="@+id/image_view">
</ImageView>
</RelativeLayout>

E. android 設置textView水平居中顯示

1、讓textView裡面的內容水平居中

設置textView屬性:android:gravity="center_horizontal"

2、讓textView控制項在它的父布局裡水平居中

設置textView屬性:android:layout_gravity="center_horizontal"

(5)android設置view居中擴展閱讀

android textView的屬性介紹

1、android:autoLink

設置是否當文本為URL鏈接/email/電話號碼/map時,文本顯示為可點擊的鏈接。可選值(none/web/email/phone/map/all)

2、android:autoText

如果設置,將自動執行輸入值的拼寫糾正。此處無效果,在顯示輸入法並輸入的時候起作用。

3、android:bufferType

指定getText()方式取得的文本類別。選項editable類似於StringBuilder可追加字元,也就是說getText後可調用append方法設置文本內容。

4、android:capitalize

設置英文字母大寫類型。此處無效果,需要彈出輸入法才能看得到,參見EditView此屬性說明。

5、android:cursorVisible

設定游標為顯示/隱藏,默認顯示。

F. Android中如何讓控制項居中

方法一:設置父布局的屬性,但是framelayout是沒有居中效果的

線性布局linearlayout和相對布局relativelayout比較常用


下面是兩個textview在LinearLayout線性布局下的效果

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text1"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text2"/>

</LinearLayout>


G. 在android中如何讓布局居中

兩種方法:

  1. 圖形化設計界面中:選中要居中的組件。在右邊的「屬性欄」(前提是你沒有把它隱藏掉)中的Gravity一欄選擇center_vertical或者center_horizontal或者center。分別表示在父布局中垂直居中、水平居中、中心。

  2. xml代碼界面當中:android:layout_gravity="center_vertical或center_horizontal或center"

H. android怎麼讓textview字體居中

android:gravity="center"是設置文字在TextView當中居中,android:layout_gravity="center"是設置在父控制項當中居中,你試試這樣子,按鈕寬度是warp content textview寬度是fill content 然後權重是1 兩個控制項高度一致應該就可以了。 textview後面再放一個相同的button 設置為不可見。

I. Android TextView 如何居中

必須寫上這個屬性,文本才能居中, 用android:layout_gravity=」center」不行; android:gravity和android:layout_gravity的區別:android:gravity用於設置View組件(比如TextView )的對齊方式,而android:layout_gravity用於設置Container組件(比如LinearLayout)的對齊方式。

J. 如何設置安卓的TextView文字居中

android:gravity="center"是設置文字在TextView當中居中,android:layout_gravity="center"是設置在父控制項當中居中,你試試這樣子,按鈕寬度是warp content textview寬度是fill content 然後權重是1 兩個控制項高度一致應該就可以了。 textview後面再放一個相同的button 設置為不可見。

閱讀全文

與android設置view居中相關的資料

熱點內容
iphone怎麼隱藏app內容 瀏覽:954
移動手機怎麼修改登錄密碼app 瀏覽:582
兩點間中點垂直線cad命令 瀏覽:32
dpdk編程開發 瀏覽:978
linux編輯文件退出命令 瀏覽:883
好看的網站 瀏覽:764
class版本為過高無法反編譯 瀏覽:467
vivo手機怎麼刪除app上的小鎖 瀏覽:462
泰國《永恆》未刪減 瀏覽:952
程序員小妹留學新加坡 瀏覽:459
app為什麼要做伺服器端app 瀏覽:608
華為電腦命令提示符在哪 瀏覽:80
法國啄木鳥網站入口 瀏覽:601
java的api怎麼打開 瀏覽:277
一本主角叫林楓的重生小說 瀏覽:809
超級靈魂解壓視頻 瀏覽:536
葉天明和柳韻 瀏覽:138
韓國愛情片在線觀看 瀏覽:745
h264壓縮工具 瀏覽:320
為什麼app總是用手機號注冊 瀏覽:430