开发者

Need to make a TextView appear next to an EditBox in the same row of a TableLayout

I have scoured the forums and Google trying to figure out what I am doing wrong here. I want a TextView to appear before an EditText on the same row. I tried doing this with a TableLayout, but the result is the EditText gets pushed off the screen in portrait orientation and you can barely see it in landscape orientation. Here is my code so far:

    <?xml version="1.0" encoding="utf-8"?>

<Button android:layout_width="wrap_content"
    android:layout_alignParentTop="true" android:layout_alignParentRight="true"
    android:layout_marginBottom="20dip" android:layout_height="wrap_content"
    android:id="@+id/about_button" android:text="@string/about_label" />

    <TextView android:text="@string/deck_size_label"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="20dip" android:textSize="24.5sp"
    android:layout_alignParentLeft="true" 
    android:layout_below="@id/about_button"/> 

    <EditText
    android:id="@+id/deck_size_textbox" android:inputType="number"
    android:layout_wid开发者_Python百科th="fill_parent" android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_toRightOf="@id/about_button" /> 
-->

Any help with this would be greatly appreciated.


If the text in your TextView is too wide, it will push the EditText off the screen. You can force the TextView and the EditText to split the available width equally using layout_weight, like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
    <TextView
        android:text="@string/land_count_label"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:gravity="left"
        android:layout_marginBottom="20dip"
        android:textSize="24.5sp"
        />
    <EditText
        android:id="@+id/land_count_textbox"
        android:inputType="number"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        />
</LinearLayout>

You can probably get better control using a RelativeLayout.


Have you tried a RelativeLayout? If you define a RelativeLayout with horizontally oriented LinearLayouts within it (for the rows), you can define XML attributes such as:

android:layout_toRightOf="@id/idoftextviewhere"

If it's still pushing the EditText box off the screen, try to also implement:

android:layout_alignParentRight
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜