visibility属性值介绍
从attrs.xml中可以看到visibility的三种可以设置的值为:visible、invisible、gone
<attr name="visibility">
<!-- Visible on screen; the default value. -->
<enum name="visible" value="0" />
<!-- Not displayed, but taken into account during layout (space is left for it). -->
<enum name="invisible" value="1" />
<!-- Completely hidden, as if the view had not been added. -->
<enum name="gone" value="2" />
</attr>
属性 | 描述 |
---|---|
visible | Visible on screen; the default value 可见的,且是默认值 |
invisible | Not displayed, but taken into account during layout (space is left for it) 不可见,但在布局过程中仍然占着位置 |
gone | Completely hidden, as if the view had not been added 隐藏的,不可见,不占用空间 |
效果测试:
代码逻辑
Activity:
public class MainActivity extends AppCompatActivity {
ImageView first_iv;
ImageView second_iv;
ImageView third_iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
first_iv = findViewById(R.id.first_iv);
second_iv = findViewById(R.id.second_iv);
third_iv = findViewById(R.id.third_iv);
findViewById(R.id.set_visible_bt).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
first_iv.setVisibility(View.VISIBLE);
}
});
findViewById(R.id.set_gone_bt).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
second_iv.setVisibility(View.GONE);
}
});
findViewById(R.id.set_invisible_bt).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
third_iv.setVisibility(View.INVISIBLE);
}
});
findViewById(R.id.reset_bt).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
first_iv.setVisibility(View.VISIBLE);
second_iv.setVisibility(View.VISIBLE);
third_iv.setVisibility(View.VISIBLE);
}
});
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/set_visible_bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第1个visible"
android:textAllCaps="false"/>
<Button
android:id="@+id/set_gone_bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第2个gone"
android:textAllCaps="false"/>
<Button
android:id="@+id/set_invisible_bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第3个invisible"
android:textAllCaps="false"/>
<Button
android:id="@+id/reset_bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全部复原"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="220dp"
android:background="#EBD6D6"
android:gravity="center">
<ImageView
android:id="@+id/first_iv"
android:layout_width="1dp"
android:layout_height="100dp"
android:layout_weight="1"
android:scaleType="centerCrop"
android:layout_margin="10dp"
android:visibility="visible"
android:src="@mipmap/panda1"/>
<ImageView
android:id="@+id/second_iv"
android:layout_width="1dp"
android:layout_height="100dp"
android:layout_weight="1"
android:layout_margin="10dp"
android:scaleType="centerCrop"
android:visibility="visible"
android:src="@mipmap/panda2"
/>
<ImageView
android:id="@+id/third_iv"
android:layout_width="1dp"
android:layout_height="100dp"
android:layout_weight="1"
android:scaleType="centerCrop"
android:layout_margin="10dp"
android:visibility="visible"
android:src="@mipmap/panda3"/>
<ImageView
android:layout_width="1dp"
android:layout_height="100dp"
android:layout_weight="1"
android:scaleType="centerCrop"
android:layout_margin="10dp"
android:visibility="visible"
android:src="@mipmap/panda4"/>
</LinearLayout>
</LinearLayout>
测试效果
注:
从GIF中可以看出,当点击invisible的时候,图片不可见了,但依然占着位置空间!
当点击gone的时候,第二个图片不可见了,位置也被其他图片平分了!
仅供参考、多多指教!