今天說下如何讓一個控件的寬度顯示時占屏幕的一般寬度,且水平居中顯示。這里拋磚引玉,給出三種實(shí)現(xiàn)方案:
1)線性布局:利用屬性android:weightSum和android:layout_weight來實(shí)現(xiàn)
2)線性布局:利用屬性android:layout_weight和隱藏?zé)o關(guān)控件的方式來實(shí)現(xiàn)
3)線性布局:通過布局文件和代碼動態(tài)修改控件的布局中地方寬度參數(shù)屬性
下面分別給出測試代碼:
1.利用android:weightSum和android:layout_weight來實(shí)現(xiàn)
布局文件如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center|top" android:background="#FFFFFF" android:weightSum="1"> <Button style="?android:attr/buttonBarStyle" android:id="@+id/idd_btn_one" android:layout_marginTop="10dp" android:background="#FF0000" android:layout_width="0dp" android:layout_height="wrap_content" android:text="寬度占屏幕的一半" android:layout_weight="0.5"/></LinearLayout>2.利用屬性android:layout_weight和隱藏?zé)o關(guān)控件的方式來實(shí)現(xiàn)布局文件如下:<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="top" android:orientation="horizontal" android:background="#FFFFFF"> <Button style="?android:attr/buttonBarStyle" android:id="@+id/idd_btn_one" android:layout_marginTop="10dp" android:layout_width="0dp" android:layout_height="wrap_content" android:text="不顯示1" android:visibility="invisible" android:layout_weight="1.03"/> <Button style="?android:attr/buttonBarStyle" android:id="@+id/idd_btn_two" android:layout_marginTop="10dp" android:background="#00FF00" android:layout_width="0dp" android:layout_height="wrap_content" android:text="寬度占屏幕的一半" android:layout_weight="2"/> <Button style="?android:attr/buttonBarStyle" android:id="@+id/idd_btn_three" android:layout_marginTop="10dp" android:layout_width="0dp" android:layout_height="wrap_content" android:text="不顯示2" android:visibility="invisible" android:layout_weight="1"/></LinearLayout>3.通過布局文件和代碼動態(tài)修改寬度屬性布局文件如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_half_of_screen_three" android:orientation="horizontal" android:gravity="center|top" android:layout_width="match_parent" android:layout_height="match_parent"> <Button style="?android:attr/buttonBarStyle" android:id="@+id/id_btn_one" android:layout_marginTop="10dp" android:background="#0000FF" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="寬度占屏幕一半" /></LinearLayout>代碼如下:import android.graphics.Point;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.ViewGroup;import android.widget.Button;import com.mobile.cdtx.blog.R;/** * created by wangwentao 2017/2/10 * 通過代碼動態(tài)修改控件的寬度占屏幕的一半 */public class HalfOfScreenActivityThree extends AppCompatActivity { @Override PRotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_half_of_screen_three); Button btn = (Button) findViewById(R.id.id_btn_one); //獲取布局參數(shù) ViewGroup.LayoutParams lp = btn.getLayoutParams(); lp.width = getScreenWidth()/2; btn.setLayoutParams(lp); } //獲取屏幕的寬度 public int getScreenWidth() { Point point = new Point(); getWindowManager().getDefaultDisplay().getSize(point); return point.x; } //獲取屏幕的高度 public int getScreenHeight() { Point point = new Point(); getWindowManager().getDefaultDisplay().getSize(point); return point.y; }}以上三種方式出現(xiàn)的效果是一樣的,由于布局比較簡單,這里就不貼圖了,代碼可以直接運(yùn)行,大家可以直接看到效果
新聞熱點(diǎn)
疑難解答