【问题标题】:How to put 50% for width如何将 50% 作为宽度
【发布时间】:2013-01-24 05:55:07
【问题描述】:

我有一个水平方向的 LinearLayout 和 2 个 ImageView,我想让 ImagesView 在宽度上填充 50% 的屏幕,以便在不同尺寸的手机或平板电脑上工作。

类似这样的:

 +-------------+   
 |_____________|     
 |      |      |  
 | 50%  | 50%  |  
 |      |      |   
 |-------------|
 |             |
 +-------------+

迄今为止最好的:

<?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:gravity="center"
    android:orientation="horizontal" >


        <ImageView
            android:id="@+id/logo_c"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/logo_animado" />

        <ImageView
            android:id="@+id/logo_t"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/logo_animado2" />


</LinearLayout>

【问题讨论】:

  • 每个 ImageView 应该有 android:layout_width="0dp"androdi:layout_weight="1"
  • 使用 android:layout_weight="" .
  • 谢谢@SherifelKhatib 解决我的问题=DD。
  • @user1744952 哈哈好点!我以为是androdi:layout_weight="":P

标签: android android-layout layout imageview


【解决方案1】:

LinearLayout 内的两个视图中编写以下代码。

android:layout_width="0dp"
layout_weight="1"

【讨论】:

  • 感谢 Chintan,非常有帮助 =D。
  • @Guilherme 这是我的荣幸... :-)
【解决方案2】:
<?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:gravity="center" 
    android:weightSum="100"
    android:orientation="horizontal" >


    <ImageView
        android:id="@+id/logo_c"
        android:layout_width="0dp" 
        android:layout_weight="50" 
        android:layout_height="wrap_content"
        android:src="@drawable/logo_animado" />

    <ImageView
        android:id="@+id/logo_t" 
        android:layout_height="wrap_content" 
        android:layout_width="0dp"    
        android:layout_weight="50" 
        android:src="@drawable/logo_animado2" />


</LinearLayout>

【讨论】:

    【解决方案3】:

    使用 android:weightSum 和 android:layout_weight

      <?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:gravity="center"
            android:weightSum="2"
            android:orientation="horizontal" >
    
    
                <ImageView
                    android:id="@+id/logo_c"
                    android:layout_width="0dp" android:layout_weight="1" 
                    android:layout_height="wrap_content"
                    android:src="@drawable/logo_animado" />
    
                <ImageView
                    android:id="@+id/logo_t"
                    android:layout_width="0dp" android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:src="@drawable/logo_animado2" />
    
    
        </LinearLayout>
    

    【讨论】:

      【解决方案4】:

      ImageView 中添加android:layout_weight="1" 并将图像视图的宽度设置为fill_parent 我认为它会解决您的问题

      但请记住,它会拉伸您的图像,因为图像视图会拉伸每个屏幕分辨率,就像您的图像一样。

      【讨论】:

        猜你喜欢
        • 2021-02-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-18
        • 2019-07-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多