当我点击一个按钮时,我需要设置这个布局,我的java就像:
Layout propLayout = (Layout) findViewById(R.id.properLayout);
public void propsBtn(View view) {
propLayout.setVisiblity(View.Visible);
}
我知道布局线完全错了!如果有人能告诉我如何正确设置,我将非常感激:)
这是我的XML:
<FrameLayout 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"
android:background="@color/mainBackGround"
tools:context="com.myapplication2.app.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp">
...contents...
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
android:id="@+id/properLayout">
<RelativeLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_gravity="bottom"
android:padding="8dp">
...contents...
</RelativeLayout>
</LinearLayout>
答案 0 :(得分:6)
DerGolem给出了正确的答案,但他随后删除了它,所以我现在在这里报告:
//First we set visibility value of interested layout either to gone, visible or invisible
android:visibility="gone"
然后在onCreate下写如:
//specify the button that has to handle visibility
ImageButton properties = (ImageButton) findViewById(R.id.propBtn);
//And the layout we want to change is visibility
final LinearLayout propLayout = (LinearLayout) findViewById(R.id.properLayout);
properties.setOnClickListener
(
new View.OnClickListener()
{
public void onClick(View v)
{
if (propLayout.getVisibility() == View.VISIBLE)
{
propLayout.setVisibility(View.INVISIBLE);
}
else
{
propLayout.setVisibility(View.VISIBLE);
}
}
}
);
答案 1 :(得分:1)
另一种切换方式,如果有人喜欢
,则用较少的线条切换// button to click
ImageButton properties = (ImageButton) findViewById(R.id.propBtn);
// and LinearLayout to toggle
final LinearLayout propLayout = (LinearLayout) findViewById(R.id.properLayout);
properties.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
propLayout.setVisibility((propLayout.getVisibility() == View.VISIBLE)
? View.INVISIBLE
: View.VISIBLE);
}
});
答案 2 :(得分:0)
试试这个:
int counter = 0;
TextVeiw tv = (TextView) findViewById(R.id.textView);
Button button = (Button ) findViewById(R.id.but);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
if(counter%2==0){
tv.setVisibility(View.Visible);//show
} else {
tv.setVisibility(View.Gone);//hide
}
}
});
答案 3 :(得分:0)
您需要在要切换的布局上设置OnClickListener()
。然后在侦听器内部检查布局是否可见。如果是,则将其可见性设置为View.INVISIBLE
。否则,您将其设置为View.VISIBLE
。