簡體   English   中英

Android ImageButton未顯示在屏幕右側

[英]Android ImageButton not displaying on right side of the screen

我無法讓ImageButton出現在屏幕的右側。 如果將ImageButton放在內部LinearLayout的上方,它將顯示在左側,但是當我將其放置在內部LinearLayout的下方時,它根本不會顯示。 以下是xml文件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="horizontal" android:padding="2dip"
    style="@style/default_style">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/layout1"
        android:orientation="vertical" >    

        <TableLayout android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:id="@+id/headerTable"
            android:stretchColumns="0,1">
            <TableRow android:layout_width="fill_parent" android:id="@+id/headerRow"
                android:layout_height="match_parent">
                <TextView android:text="Fullname" android:id="@+id/fullname"
                    android:layout_height="wrap_content" android:layout_gravity="left"
                    android:layout_width="fill_parent" android:textStyle="bold"
                    style="@style/default_style"></TextView>
                <TextView android:text="ContentTimestamp" android:id="@+id/content_timestamp"
                    android:layout_height="wrap_content" android:layout_gravity="right"
                    android:layout_width="fill_parent"
                    style="@style/default_style"></TextView>
                <ImageView android:id="@+id/hasGeoLocation"
                    android:layout_width="wrap_content" android:layout_height="wrap_content"
                    android:src="@drawable/map" android:paddingLeft="2dip" android:visibility="visible">
                </ImageView>    
            </TableRow>

        </TableLayout>

        <TextView android:id="@+id/content"
            android:text="Content blah blah blah blah blah blah blah blah blah"
            android:layout_width="fill_parent" android:gravity="center_vertical"
            android:textSize="16dip" android:layout_height="wrap_content"
            android:paddingLeft="5dip"
            style="@style/default_style"> </TextView>

    </LinearLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >                      
        <ImageButton android:id="@+id/hasAudio"
            android:layout_width="50px"
            android:layout_height="50px"
            android:layout_toRightOf="@+id/layout1"
            android:gravity="right"
            android:src="@drawable/play"
            android:visibility="visible" />     
    </RelativeLayout>
</LinearLayout>

這是另一個可能的解決方案。 您可以將內部 LinearLayout的權重設置為一個值,例如1。這將允許ImageButton在父級約束范圍內而不是外部約束范圍內進行渲染。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="horizontal" android:padding="2dip"
    style="@style/default_style">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center_vertical" 
        android:layout_weight="1">
        <TableLayout android:layout_width="fill_parent"
    ...

參考文檔: http : //developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html#weight

您需要將ImageButton放在布局中。 它不是渲染,因為您沒有告訴它在哪里渲染。

您可以按照自己想要的方式定位它的一種方法是,在您擁有的線性布局之后添加相對布局。 使用相對布局將圖像放置在右側http://developer.android.com/reference/android/widget/RelativeLayout.html

以下文章提供了一些有關在RelativeLayout中放置事物的信息http://www.higherpass.com/Android/Tutorials/Exploring-Android-Linearlayout-And-Relativelayout/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM