一.先看看为什么有时候虚线画不出来
以前布局中画虚线,设置height不一样,导致有时候虚线有时候显示的出来,有时候显示不出来,今天就来分析一下为什么。
布局文件
drawable/dotted_line.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line" >
<stroke
android:dashGap="50dp"
android:dashWidth="30dp"
android:width="1dp"
android:color="@color/abc_input_method_navigation_guard"
/>
</shape>
MainActivity.xml
<RelativeLayout xmlns: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"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv_text"
android:gravity="center"
android:layout_centerInParent="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="下面是虚线"/>
<!--在真机上View的高度必须大于dotted_line的stroke的width至少0.3dp否则显示不出来 -->
<!--在模拟器上View的高度必须大于dotted_line的stroke的width至少0.5dp,否则显示不出来-->
<View
android:layout_below="@id/tv_text"
android:id="@+id/view_line_change"
android:layout_width="fill_parent"
android:layout_height="1.3dp"
android:background="@drawable/dotted_line"
android:layerType="software"
/>
</RelativeLayout>
总结:
在真机上View的高度必须大于dotted_line的stroke的width至少0.3dp否则显示不出来
在模拟器上View的高度必须大于dotted_line的stroke的width至少0.5dp,否则显示不出来
二.看看View这个布局中的控件的宽高到底是多少
布局文件一
linearlayout_text_view_height.xml
<?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:orientation="vertical"
>
<TextView
android:gravity="center"
android:id="@+id/tv_title"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:text="我是头"/>
<View
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
效果一
布局文件二
relativelayout_text_view_height.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:gravity="center"
android:id="@+id/tv_title"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:text="我是头"/>
<!--这时候即使是包裹内容,view填满了整个屏幕-->
<View
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
效果二
总结:
可以看出,即使设置了wrap_content,宽高实际还是match_parent
三.动态的用代码来写布局(会涉及到上面view的使用)
布局文件
Activity_main.xml
RelativeLayout xmlns: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"
tools:context=".MainActivity">
<Button
android:layout_centerHorizontal="true"
android:id="@+id/bt_makevisible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="让虚线以下的东西出现"
/>
<Button
android:layout_toLeftOf="@id/bt_makevisible"
android:layout_centerHorizontal="true"
android:id="@+id/bt_change_position"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="改变位置"
/>
<TextView
android:id="@+id/tv_text"
android:gravity="center"
android:layout_centerInParent="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="下面是虚线"/>
<!--这里的View的高度必须大于dotted_line的stroke的width至少0.2dp否则显示不出来 -->
<View
android:layout_below="@id/tv_text"
android:id="@+id/view_line_change"
android:layout_width="fill_parent"
android:layout_height="1.3dp"
android:visibility="gone"
android:background="@drawable/dotted_line"
android:layerType="software"
/>
<TextView
android:layout_below="@id/view_line_change"
android:gravity="center"
android:id="@+id/tv_one"
android:visibility="gone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="one"/>
<TextView
android:id="@+id/tv_two"
android:layout_below="@id/tv_one"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="two"/>
</RelativeLayout>
代码:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button bt_change;
private Button bt_makevisible;
private TextView tv_one;
private View view_dotted_line;
private boolean showflag = false;
private TextView tv_two;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_change = (Button) findViewById(R.id.bt_change_position);
bt_makevisible = (Button) findViewById(R.id.bt_makevisible);
tv_one = (TextView) findViewById(R.id.tv_one);
tv_two = (TextView) findViewById(R.id.tv_two);
view_dotted_line = findViewById(R.id.view_line_change);
bt_change.setOnClickListener(this);
bt_makevisible.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.bt_makevisible:
if (showflag) {
//消失
System.out.println("xcq消失");
tv_one.setVisibility(View.GONE);
view_dotted_line.setVisibility(View.GONE);
} else {
//不消失
System.out.println("xcq不消失");
tv_one.setVisibility(View.VISIBLE);
view_dotted_line.setVisibility(View.VISIBLE);
}
showflag = !showflag;
break;
case R.id.bt_change_position:
//1.改变 textview(one) 的位置
//RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams 设置的父亲的宽高(也就是容器的宽高)
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams
(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp.setMargins(0, CommonUtils.Dp2Px(MainActivity.this, 100), 0, 0);
tv_one.setGravity(Gravity.RIGHT);//设置内部内容
tv_one.setLayoutParams(rlp);
//2.改变 view(虚线)的位置 让他随着 上面的textview改变
//view这的height不能设置wrap_content,否则他会填充屏幕剩下的所有空间!!!!!
RelativeLayout.LayoutParams rlp2 = new RelativeLayout.LayoutParams
(RelativeLayout.LayoutParams.WRAP_CONTENT, CommonUtils.Dp2Px(MainActivity.this, 3));
//设置自己的位置
rlp2.addRule(RelativeLayout.BELOW, R.id.tv_one);
view_dotted_line.setLayoutParams(rlp2);
//3.改变 textview(tow) 的位置
RelativeLayout.LayoutParams rlp3 = new RelativeLayout.LayoutParams
(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp3.addRule(RelativeLayout.BELOW, R.id.view_line_change);
tv_two.setLayoutParams(rlp3);
break;
}
}
}
效果:
总结:
1.可以看出开始的时候textview(two)一开始是textview(下面是虚线),点击button(显示虚线一下的),就把其他的控件显示出来了
2.在点击了一个button(改变位置),位置就发生变化,textview(two)就不在是在textview(one)下面了(通过addrule来控制),而且可以看到textview(one)的内容的位置也变了(通过setgravity来改变)
3.可以看到点击button(改变位置后),textview(one),textview(two)中的内容的位置都发生了变化
a.对于textview(one)
(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT),还是设置match_parent,但是布局中的gravity=center通过setgravity改成了right,所以发生了改变
b.对于textview(two)
(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT),width在布局文件中的match_parent,被改成了wrap_content,所以gravity = center就不能居中了,因为现在是包裹内容了
注意:
如果这段代码(也就是虚线的设置)
RelativeLayout.LayoutParams rlp2 = new RelativeLayout.LayoutParams
(RelativeLayout.LayoutParams.WRAP_CONTENT, CommonUtils.Dp2Px(MainActivity.this, 3));
//设置自己的位置
rlp2.addRule(RelativeLayout.BELOW, R.id.tv_one);
view_dotted_line.setLayoutParams(rlp2);
中的CommonUtils.Dp2Px(MainActivity.this, 3) 改成wrap_content,效果变成这样了
效果:
(RelativeLayout.LayoutParams.WRAP_CONTENT, CommonUtils.Dp2Px(MainActivity.this, 3)); 就是设置当前view容器的宽高