今天说下如何让一个控件的宽度显示时占屏幕的一般宽度,且水平居中显示。这里抛砖引玉,给出三种实现方案:
1)线性布局:利用属性android:weightSum和android:layout_weight来实现
2)线性布局:利用属性android:layout_weight和隐藏无关控件的方式来实现
3)线性布局:通过布局文件和代码动态修改控件的布局中地方宽度参数属性
下面分别给出测试代码:
1.利用android:weightSum和android:layout_weight来实现
布局文件如下:
<?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和隐藏无关控件的方式来实现
布局文件如下:
<?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.通过布局文件和代码动态修改宽度属性
布局文件如下:
<?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
* 通过代码动态修改控件的宽度占屏幕的一半
*/
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);
//获取布局参数
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;
}
}
以上三种方式出现的效果是一样的,由于布局比较简单,这里就不贴图了,代码可以直接运行,大家可以直接看到效果