2015-04-04 210 views
2

我刚开始学习Android。很少有混淆我已经关于XML布局如何以编程方式在Android中隐藏布局/视图

  1. 是我在布局定义所有的观点本质上的膨胀或它们是可选的?假设我在一个视图组中有两个不同的视图,但我想 只有第一个或只有第二个有条件地使用。可能吗?

  2. 如何动态创建视图处理layout.XML 文件?

  3. 如果我希望收到的消息以红色显示并以黑色发送消息,我该怎么做?

回答

0

你可以在xml中设置android:visibility =“gone”或者通过代码设置setVisibility(View.gone);对于更改文本颜色,您可以设置android:text color =“#000000”或通过代码setTextColor();

3

除非以编程方式显示视图,否则可以在XML布局文件中包含视图中的视图。只需在XML文件中使用“android:visible =”gone“或”android:visible =“invisible”即可。

举例来说,包括我在我的布局文件下面开始,但它不可见:

<LinearLayout 
     android:id="@+id/pnlLatLong" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:visibility="gone" 
     > 
     <TextView 
      android:id="@+id/lblLatLng" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/lat_long" 
      /> 
     <EditText 
      android:id="@+id/txtLatitude" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:inputType="numberDecimal|numberSigned" 
      /> 
     <EditText 
      android:id="@+id/txtLongitude" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:inputType="numberDecimal|numberSigned" 
      /> 
    </LinearLayout> 

在Java代码中,当代码逻辑决定它应该是可见的,我编程改变能见度:

View v = findViewById(R.id.pnlLatLng); 
    v.setVisibility(View.VISIBLE); 
+1

重要的是要注意INVISIBLE和GONE不是同义词。 GONE将实际上从布局中移除视图,根据需要调整其他视图,而INVISIBLE将简单地隐藏视图,但不会更改其边界/渲染大小。 – Nicklas 2015-04-04 20:54:03

相关问题