- gravity和layout_gravity的区别
android:gravity 属性是对该view中内容的限定.
android:layout_gravity是用来设置该view相对与父view 的位置.
- 在代码中如何设置
android:gravity 对应的是 setGravity()
android:layout_gravity 对应的是 LayoutParam.gravity
The gravity to apply with the View to which these layout parameters
* are associated.
* are associated.
也就是属于 LayoutParam的作用区域(对父view)
实例:
public class TestViewActivity
extends AppCompatActivity {
@Override
protected void onCreate( @Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState) ;
LinearLayout picContainer= new LinearLayout( this) ;
picContainer.setLayoutParams( new ViewGroup.LayoutParams(ViewGroup.LayoutParams. MATCH_PARENT , DPIUtil.
@Override
protected void onCreate( @Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState) ;
LinearLayout picContainer= new LinearLayout( this) ;
picContainer.setLayoutParams( new ViewGroup.LayoutParams(ViewGroup.LayoutParams. MATCH_PARENT , DPIUtil.