相对布局高度“wrap_content”不包装内容
我正在制作一个布局来复制自定义对话框。对于对话框的底部,即使我将 RelativeLayout
高度设置为 wrap_content<,托管积极、中性和消极按钮的
RelativeLayout
也具有扩展的高度。 /代码>。
我的布局 XML 如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/TextView_Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dip"
android:text="Title"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
<LinearLayout
android:id="@+id/linearLayout3"
android:layout_width="match_parent"
android:layout_height="1dip"
android:layout_marginBottom="10dip"
android:layout_marginLeft="0dip"
android:layout_marginRight="0dip"
android:layout_marginTop="10dip"
android:background="@android:drawable/divider_horizontal_dim_dark" >
</LinearLayout>
<TextView
android:id="@+id/TextView_Message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:text="TextView" />
<RelativeLayout
android:id="@+id/relativeLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/bottom_bar" >
<Button
android:id="@+id/Button_Positive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/Button_Neutral"
android:layout_centerVertical="true"
android:text="Button" />
<Button
android:id="@+id/Button_Neutral"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Button" />
<Button
android:id="@+id/Button_Negative"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/Button_Neutral"
android:layout_centerVertical="true"
android:text="Button" />
</RelativeLayout>
Java活动代码:
public class MainActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog);
// Assign content references
TextView message = (TextView) findViewById(R.id.TextView_Message);
message.setText("Are you sure you want to sign out?");
}
}
I'm making a layout to replicate a custom dialog. For the bottom portion of my dialog, the RelativeLayout
hosting the positive, neutral, and negative buttons has an extended height even though I set the RelativeLayout
height to wrap_content
.
My layout XML is below:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/TextView_Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dip"
android:text="Title"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
<LinearLayout
android:id="@+id/linearLayout3"
android:layout_width="match_parent"
android:layout_height="1dip"
android:layout_marginBottom="10dip"
android:layout_marginLeft="0dip"
android:layout_marginRight="0dip"
android:layout_marginTop="10dip"
android:background="@android:drawable/divider_horizontal_dim_dark" >
</LinearLayout>
<TextView
android:id="@+id/TextView_Message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:text="TextView" />
<RelativeLayout
android:id="@+id/relativeLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/bottom_bar" >
<Button
android:id="@+id/Button_Positive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/Button_Neutral"
android:layout_centerVertical="true"
android:text="Button" />
<Button
android:id="@+id/Button_Neutral"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Button" />
<Button
android:id="@+id/Button_Negative"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/Button_Neutral"
android:layout_centerVertical="true"
android:text="Button" />
</RelativeLayout>
Java Activity code:
public class MainActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog);
// Assign content references
TextView message = (TextView) findViewById(R.id.TextView_Message);
message.setText("Are you sure you want to sign out?");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在 Eclipse(如前所述)或 Motorola Defy MB525 上正确渲染布局时没有遇到任何问题。但是,如果我将活动的主题设置为 Theme.Dialog,它会显示为纵向挤压,导致只有中性按钮可见。您还可以对问题中提出的布局进行一些简化,例如标题 TextView 的不必要嵌套以及(ab)使用 LinearLayout 作为水平分隔线。
我改变了一些小事情,形成了下面的布局。也许你可以尝试一下?
顺便说一句,您也可以将此布局用于“真实”对话框,而不是使用活动来模拟对话框。 Android 开发网站上有一个很好的创建自定义对话框的示例 。
I didn't have any problems rendering the layout correctly in Eclipse (as commented earlier), or on a Motorola Defy MB525. However, if I would set the activity's theme to Theme.Dialog, it was showing up squeezed length-wise, resulting in only the neutral button being visible. There are also some simplifications you could do to the layout as presented in your question, e.g. unnecessary nesting of the title TextView and (ab)using a LinearLayout for a horizontal divider.
I changed some small things resulting in the layout below. Perhaps you can give that a try?
By the way, in stead of using an activity to emulate a dialog, you could also just use this layout for a 'real' dialog. There's a good example on creating custom dialogs on the Android dev site.