Android 以编程方式在按钮上应用setLayoutParams会导致整个布局混合和异常
有什么提示吗 应用答案后更新1: 我正在使用以下代码将按钮设置为不可见Android 以编程方式在按钮上应用setLayoutParams会导致整个布局混合和异常,android,android-layout,android-xml,android-button,Android,Android Layout,Android Xml,Android Button,有什么提示吗 应用答案后更新1: 我正在使用以下代码将按钮设置为不可见 modifyButton.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); 使用 Bundle extras; extras = getIntent().getExtras(); String answer = extras.getString("answer"); if(answer.equa
modifyButton.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
使用
Bundle extras;
extras = getIntent().getExtras();
String answer = extras.getString("answer");
if(answer.equalsIgnoreCase("yes")
{
insertButton.setEnabled(true);
insertButton.setText("INSERT");
removeButton.setVisibility(View.INVISIBLE);
}
else
{
//whatever
}
*删除此*
和设置
modifyButton.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
在两个按钮中//我已经修改了您的代码,现在请尝试此按钮,您不需要设置LayoutParams()。
android:layout_width="0dp"
如果(回答:相等信号情况(“是”)
{
insertButton.setEnabled(真);
insertButton.setText(“插入”);
removeButton.setVisibility(View.GONE);
}
其他的
{
//随便
}
使用setVisibility(查看。消失)
。这应该就够了。@Android Developer这样做了,它并没有调整到整个屏幕的大小,只是第二个按钮消失了,第一个按钮保持不变的大小,即屏幕的一半。你是否也通过编程设置布局参数?@Android Developer否。只是宽度,实际上我应用了下面提供的唯一答案。你能举个例子吗?你什么时候隐藏“删除”按钮,因为我刚刚在另一个onClickListener
中实现了上面给出的想法,它正在工作。@birahZalavadia这样做,删除了第二个按钮,但是第一个按钮现在占据了屏幕的一半,就像它在隐藏第二个按钮之前所占据的一样。工作。应该有已使用View.GONE而不是View.Invisible。若要了解有关差异的更多信息,请查看此链接
modifyButton.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
android:layout_width="0dp"
// I Have modify your code now try this one and you don't required set LayoutParams ().
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" >
<Button
android:id="@+id/insertButtonVIEW"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="insert" />
<Button
android:id="@+id/removeButtonVIEW"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="remove" />
</LinearLayout>
</RelativeLayout>
if(answer.equalsIgnoreCase("yes")
{
insertButton.setEnabled(true);
insertButton.setText("INSERT");
removeButton.setVisibility(View.GONE);
}
else
{
//whatever
}