草庐IT

Android View 相同的宽度和高度

coder 2023-12-25 原文

以下是我的 xml 代码。在我的 LinearLayout 中。我有两个 subview ,它们使用 weightSum 对齐。我有一个 ImageView,由于 weightSum,其宽度正确对齐,但问题在于它的高度。我希望它的高度与其宽度的尺寸相同。 有没有不使用静态 dp 值的解决方案。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:paddingBottom="5dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp">

    <LinearLayout
        android:background="@drawable/background_gradien_yellow"
        android:weightSum="10"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

            <ImageView
                android:layout_weight="6"
                android:id="@+id/startbtn"
                android:textStyle="bold"
                android:alpha="0.8"
                android:textColor="#fff"
                android:text="@string/start"
                android:layout_centerInParent="true"
               android:background="@drawable/background_gradient_circular"
                android:layout_width="match_parent"
                android:layout_height="150dp"/>


        <LinearLayout
            android:layout_marginTop="20dp"
            android:layout_marginBottom="20dp"
            android:orientation="vertical"
            android:layout_weight="3"
            android:layout_width="match_parent"
            android:layout_height="match_parent">


            <TextView
                android:textColor="@color/White"
                android:id="@+id/tv_challange_title"
                android:textSize="20sp"
                android:text="Early-Bird Challange"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

            <TextView
                android:id="@+id/tv_challange_desc"
                android:alpha="0.5"
                android:textColor="@color/White"
                android:textSize="15sp"
                android:layout_marginTop="5dp"
                android:text="Take 100 steps five times within.. "
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>


        </LinearLayout>

    </LinearLayout>

</RelativeLayout>

最佳答案

通过使用自定义 ImageView 找到了解决方案。 这是最好的解决方案。在自定义 ImageView 类中将高度设置为与宽度相同。

Java代码:

public class SquareImageView extends ImageView {

    public SquareImageView(Context context) {
        super(context);
    }

    public SquareImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SquareImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int width = getMeasuredWidth();
        setMeasuredDimension(width, width);
    }

}

XML代码

         <com.mypakage.utils.SquareImageView
            android:background="@drawable/fitbit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

关于Android View 相同的宽度和高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38068751/

有关Android View 相同的宽度和高度的更多相关文章

  1. ruby - 如果指定键的值在数组中相同,如何合并哈希 - 2

    我有一个这样的哈希数组:[{:foo=>2,:date=>Sat,01Sep2014},{:foo2=>2,:date=>Sat,02Sep2014},{:foo3=>3,:date=>Sat,01Sep2014},{:foo4=>4,:date=>Sat,03Sep2014},{:foo5=>5,:date=>Sat,02Sep2014}]如果:date相同,我想合并哈希值。我对上面数组的期望是:[{:foo=>2,:foo3=>3,:date=>Sat,01Sep2014},{:foo2=>2,:foo5=>5:date=>Sat,02Sep2014},{:foo4=>4,:dat

  2. ruby-on-rails - 优雅的 Rails : multiple routes, 相同的 Controller Action - 2

    让多条路线去同一条路的最优雅的方式是什么ControllerAction?我有:get'dashboard',to:'dashboard#index'get'dashboard/pending',to:'dashboard#index'get'dashboard/live',to:'dashboard#index'get'dashboard/sold',to:'dashboard#index'这很丑陋。有什么“更优雅”的建议吗?一个类轮的奖励积分。 最佳答案 为什么不只有一个路由和一个Controller操作,并根据传递给它的参数来

  3. ruby-on-rails - 获取并发布相同匹配项的请求 - 2

    在我的路线文件中我有:match'graphs/(:id(/:action))'=>'graphs#(:action)'如果是GET请求(工作)或POST请求(不工作),我想匹配它我知道我可以使用以下方法在资源中声明POST请求:post'/'=>:show,:on=>:member但是我怎样才能为比赛做到这一点呢?谢谢。 最佳答案 如果你同时想要POST和GETmatch'graphs/(:id(/:action))'=>'graphs#(:action)',:via=>[:get,:post]编辑默认值可以设置如下match'g

  4. ruby-on-rails - 多次选择一个随机数,但绝不会两次选择相同的随机数 - 2

    这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:HowdoIgeneratealistofnuniquerandomnumbersinRuby?我想做的事:Random.rand(0..10).timesdoputsRandom.rand(0..10)end但如果随机数已经显示过,则无法再次显示。如何最轻松地做到这一点?

  5. ruby-on-rails - 如何通过 POST 发送多个相同的键/参数? - 2

    如果我必须在一个HTTP请求中发送一堆post参数,所有这些参数都具有相同的名称,我该如何构建要发布的data对象?想象一个带有一些复选框的表单,它们都具有相同的name属性但具有不同的值(如果它们被选中):我想用ruby​​构建它(但它需要根据在表单上选择的内容动态创建):data={"color"=>"red","color"=>"green","color"=>"blue"}然后将数据发送到某个URL:Net::HTTP.post_form(url,data)我无法控制接收端,所以我必须发送它期望接收的参数。怎么办? 最佳答案

  6. ruby - 使2个数组大小相同 - 2

    2个数组的数组:a=[[1,2],[22,11],[18,9]]b=[[1,81]]用[0,0]填充第二个的最佳方法是什么,以便它们具有相同的大小? 最佳答案 b.fill(b.size..a.size-1){[0,0]} 关于ruby-使2个数组大小相同,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/29725615/

  7. ruby - 优雅的链式 'or' 用于测试 Ruby 中的相同变量 - 2

    怎样说才是明智的呢?if@thing=="01"or"02"or"03"or"04"or"05"(数字包含在数据类型字符串的列中。) 最佳答案 制作数组并使用.include?if["01","02","03","04","05"].include?(@thing)如果值真的都是连续的,你可以使用像(1..5).include?这样的范围对于字符串,你可以使用:if("01".."05").include?(@thing) 关于ruby-优雅的链式'or'用于测试Ruby中的相同变量,我

  8. ruby - 正则表达式在 Ruby 中捕获相同数字的组 - 2

    是否可以在Ruby上使用正则表达式捕获字符串中所有相同数字的grous?我不熟悉正则表达式。我的意思是:"1112234444"上的正则表达式将生成["111","22","3","4444"]我知道,我可以使用(\d)(\1*),但它在每场比赛中只给我2个组。["1","11"],["2","2"],["3",-],["4","444"]如何在每场比赛中获得一组?谢谢。 最佳答案 在这里,试一试:((\d)\2*) 关于ruby-正则表达式在Ruby中捕获相同数字的组,我们在Stack

  9. ruby - 如何批量检查文件内容是否相同 - 2

    我想使用Ruby检查数千对文件中的每对文件是否包含相同的信息。有人能指出我正确的方向吗? 最佳答案 require'fileutils'FileUtils.compare_file('file1','file2')当且仅当文件file1和file2相同时返回true。 关于ruby-如何批量检查文件内容是否相同,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/33769865/

  10. ruby - 在 Ruby 中删除相同的对象? - 2

    我目前正在编写一个Ruby应用程序,用于在Twitter上搜索各种内容。我将要面对的问题之一是时间上彼此非常接近的搜索之间的共享结果。结果以对象数组的形式返回,每个对象都是一条推文。我知道ruby​​中的Array.uniq方法,它返回一个删除了所有重复项的数组。我的问题是这样的。只要这些对象指向内存中的相同空间或它们包含相同的信息,uniq方法是否会删除重复项?如果是前者,根据内容从数组中删除重复项的最佳方法是什么? 最佳答案 Doestheuniqmethodremoveduplicatesinsofarastheseobjec

随机推荐