2012-02-07 147 views
1

我正在创建一个工具栏,用于在单击工具栏上的按钮时切换按钮的可见性。所以,如果用户点击“绘图”按钮,“绘图”按钮上方将出现不可见的按钮“铅笔”和“笔”,如果再次点击“绘图”按钮,“铅笔”和“笔”将成为再次可见。Android中的按钮可见性问题

在我的xml文件我已经设置了一些按钮的公开程度是“看不见”的,所以当我启动,他们将无法看到活动。这部分是直线前进。

btnDrawLine的.xml文件 - (更新@ 12:21)

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" > 

<com.odhranlynch.testSection.UserInterface 
    android:id="@+id/UserInterface" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_alignParentTop="true" /> 

<Button 
    android:id="@+id/btnDraw" 
    android:layout_width="80dip" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:text="Draw" /> 

<Button 
    android:id="@+id/btnDrawLine" 
    android:layout_width="80dip" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/btnDraw" 
    android:layout_alignParentLeft="true" 
    android:visibility="visible" 
    android:text="Line" /> 

<Button 
    android:id="@+id/btnDrawCurve" 
    android:layout_width="80dip" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/btnDrawLine" 
    android:layout_alignParentLeft="true" 
    android:visibility="visible" 
    android:text="Curve" /> 

<Button 
    android:id="@+id/btnCutout" 
    android:layout_width="80dip" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_toRightOf="@+id/btnDraw" 
    android:text="Cutout" /> 

<Button 
    android:id="@+id/btnCutInner" 
    android:layout_width="80dip" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/btnDraw" 
    android:layout_toRightOf="@+id/btnDraw" 
    android:visibility="visible" 
    android:text="Inner" /> 

<Button 
    android:id="@+id/btnCutOutter" 
    android:layout_width="80dip" 
    android:layout_height="wrap_content" 
    android:layout_alignBaseline="@+id/btnDrawCurve" 
    android:layout_alignBottom="@+id/btnDrawCurve" 
    android:layout_toLeftOf="@+id/btnCancel" 
    android:visibility="visible" 
    android:text="Outter" /> 

<Button 
    android:id="@+id/btnCancel" 
    android:layout_width="80dip" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_toLeftOf="@+id/btnFinish" 
    android:text="Cancel" /> 

<Button 
    android:id="@+id/btnFinish" 
    android:layout_width="80dip" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 
    android:text="Finish" /> 

</RelativeLayout> 

接下来,当用户点击一个按钮,是可见的,我会就像看不见的按钮一样。

这是事情,他们不会再出现!大声笑我很困惑它。

,我会很感激,如果有人将是一种足以使光棚到这对我来说:)

testActivity.java

package com.odhranlynch.testSection; 

import com.odhranlynch.testSection.R; 
import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 

public class testActivity extends Activity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.new_product); 

    // Find buttons and give them a name. 
    final View btnDraw = findViewById(R.id.btnDraw); 
    final View btnCutOut = findViewById(R.id.btnCutout); 
    final View btnDrawLine = findViewById(R.id.btnDrawLine); 
    final View btnDrawCurve = findViewById(R.id.btnDrawCurve); 
    final View btnCutInner = findViewById(R.id.btnCutInner); 
    final View btnCutOutter = findViewById(R.id.btnCutOutter); 


    //Draw Button clicked (UI Toolbar). 
    btnDraw.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) { 
      //Treat button as a toggle button 
      //So if a sub-button (e.g. Draw Line) is visible, then we know the button has 
      //been toggled to visible so lets now make it invisible. 

      if (btnDrawLine.getVisibility()== View.VISIBLE) { 
       //Its visible. 
       btnDrawLine.setVisibility(View.INVISIBLE); 

       btnDrawCurve.setVisibility(View.INVISIBLE); 
       Log.d("TAG", "INVISIBLE"); 
      } else { 
       // Either gone or invisible 
       btnDrawLine.setVisibility(View.VISIBLE); 
       btnDrawCurve.setVisibility(View.VISIBLE); 
       Log.d("TAG", "VISIBLE"); 
      } 
     } 
    });  

} 
} 

作为进一步的提点,如果我将按钮的可见性设置为在.xml文件中可见我可以在运行时切换可见性!

同样,我将不胜感激一些帮助:)

+0

是否按预期打印日志消息?每个按钮时钟上的可见,可靠,可见...等。 – 2012-02-07 12:04:37

+0

是的,所以我知道切换按钮的作用。 – Odhran 2012-02-07 12:05:59

+0

嗯,你有没有尝试用View.GONE替换btnDrawCurve.setVisibility(View.INVISIBLE)?只是大声思考。 – 2012-02-07 12:14:11

回答

0

尝试将View.INVISIBLE更换为View.GONE

0

你的代码工作很好..

XML文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" /> 

<Button 
android:id="@+id/btnDrawLine" 
android:layout_width="80dip" 
android:layout_height="wrap_content" 
android:layout_above="@+id/btnDraw" 
android:layout_alignParentLeft="true" 
android:visibility="invisible" 
android:text="Line" /> 

<Button 
    android:id="@+id/draw" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="draw" /> 

</LinearLayout> 

活动

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 

public class DrawCanvasActivity extends Activity { 
private static final String Number1 = "9686801147"; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    final View btnDrawLine = findViewById(R.id.btnDrawLine); 


    Button btnDraw = (Button) findViewById(R.id.draw); 
    btnDraw.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) { 
      if (btnDrawLine.getVisibility()== View.VISIBLE) { 
       btnDrawLine.setVisibility(View.INVISIBLE); 
       Log.d("TAG", "INVISIBLE"); 
      } else { 
       btnDrawLine.setVisibility(View.VISIBLE); 
       Log.d("TAG", "VISIBLE"); 
      } 
     } 
    });  
} 
} 

的当点击绘图按钮时出现按钮线。猜测在视图格式中可能存在问题你的代码。

+0

嗯,多数民众赞成在奇怪的。 SurfaceView是否会对此产生任何干扰?我创建一个在我的代码中使用的。我会更新我的.xml片段... – Odhran 2012-02-07 12:16:35

+1

尝试为表面视图设置固定的宽度和高度。绘制按钮在surfaceview下方,drawline在draw上面。所以有可能Surfaceview与Drawline按钮重叠。尝试将它固定在Draw按钮下方。这是否工作? – user936414 2012-02-07 12:26:25

+0

非常感谢user936414 :)我现在正在工作。虽然,我一定会看看你的建议:) – Odhran 2012-02-07 12:33:34

0

如果您不想使用消失/可见,只需在想要隐藏的按钮周围添加一个LinearLayout。 LinearLayout将有一个layout_width = wrap_content;并且您引用其他元素在此布局中的位置。

在您自由更改可见性后,您的按钮消失/可见。