标签: android-layout-weight

在onMeasue中处理自定义视图的权重

我有一个我正在创建的自定义视图,它将缩放其子项的字体大小TextViews以适应所有设置的宽度,因此每个都在它自己的行上.显然,它需要宽度来计算出来.我这样被覆盖了onMeasure():

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  int widthSize = MeasureSpec.getSize(widthMeasureSpec);
  int lineWidth = widthSize - getPaddingLeft() - getPaddingRight();

  // Change the text size until the largest line of text fits.
  while (lineWidth < calculateTextWidth()) {
    for (TextView textView : this.childViews) {
      float newSize = textView.getTextSize() - 1;
      textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, newSize);
    }
  }

  super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
Run Code Online (Sandbox Code Playgroud)

calculateTextWidth()计算最大文本行的宽度,一切都适用.这段代码适用于FILL_PARENT和WRAP_CONTENT宽度,但是当我尝试给组件一个权重并让它自动设置它的权重时,它就会搞砸 - FILL_PARENT返回0,就像这样WRAP_CONTENT.这给出了一个很好的Activity Not Responding错误,因为lineWidth总是小于MeasureSpec.getSize(widthMeasureSpec)将返回的错误,因此0循环将永远运行.我使用的XML代码基本上是:

<com.my.widgets.ScalingTextViewGroup
  android:layout_width="0dp"
  android:layout_height="wrap_content"
  android:layout_weight="1"
  android:orientation="vertical"
  android:gravity="center_horizontal" …
Run Code Online (Sandbox Code Playgroud)

android android-custom-view android-layout android-layout-weight

6
推荐指数
1
解决办法
3364
查看次数

Android如何设置RemoteView的权限?

我需要知道允许我设置RemoteView权重的代码.

我尝试使用此代码,但这不起作用:

RemoteViews remoteViews = new RemoteViews(c.getPackageName(), R.layout.notification);
remoteViews.setInt(R.id.ll_notification, "setWeight", 12);
Run Code Online (Sandbox Code Playgroud)

有办法做到这一点?非常感谢....

android remoteview android-layout-weight

6
推荐指数
1
解决办法
694
查看次数

灵活的水平布局,带有`layout_weight`和`maxWidth`

我有一个TextView和一个正方形ImageView,我想以水平线性布局显示.每个应该占父视图宽度的一半,高度应该适合内容(即图像).文本应垂直居中.

附加约束是图像不应超过给定maxWidth(= maxHeight),并且应该使超出的宽度可用TextView.显然,这与上面的50/50规则相冲突.有没有办法优先考虑约束,即允许图像占用一半的空间,除非它超过给定的大小?

期望的结果

这些是我尝试过的布局:

第一个很好地将左侧拉伸到可用空间.但是图像占用宽度的一半以上,因为layout_weight未指定(如下图所示).

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center_vertical">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Some text."/>
    </LinearLayout>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxWidth="200dp"
        android:adjustViewBounds="true"
        android:src="@drawable/image" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

没有<code>layout_weight</code>到<code>ImageView</code>它始终把宽度的一半,而忽略了<code>maxWidth</code>(见下面的图片).</p>

<pre><code>    <ImageView
        android:layout_width=Run Code Online (Sandbox Code Playgroud)

使用<code>ImageView</code>在另一个<code>LinearLayout</code>中<code>maxWidth</code>再次启用,但封闭<code>LinearLayout</code>仍然占用一半的可用空间(见下图).</p>

<pre><code>    <LinearLayout
        android:layout_width=Run Code Online (Sandbox Code Playgroud)

使用封闭的<code>RelativeLayout</code>带有不可见视图的a作为中心分隔符,但是将分区锁定为50/50(或放置分隔符的任何位置).</p> </div>
        <p>
          <a href=xml android android-layout android-layout-weight

6
推荐指数
1
解决办法
2342
查看次数

Android在垂直方向的LinearLayout中设置TextView百分比的最大宽度

我想设置layout_weightTextViewtv_long_text至80%,在以下LinearLayout垂直方向.

<LinearLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:orientation="vertical">
    <TextView
            android:id="@+id/tv_short_text"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            tools:text="short text" />
    <TextView
            android:id="@+id/tv_long_text"
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:layout_weight="0.8"
            tools:text="a pretty long text" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

上述方法无法正常工作,因为textview父级的方向是垂直的.

因此,我尝试android:layout_width="match_parent"在xml中设置,然后通过获取测量的宽度在运行时设置宽度,然后将宽度设置为80%但是getMeasuredWidth给出0.

int measuredWidth = longTextView.getMeasuredWidth();
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) longTextView.getLayoutParams();
params.width = (int) (measuredWidth * 0.8);
longTextView.setLayoutParams(params);
Run Code Online (Sandbox Code Playgroud)

我也尝试在运行时设置layout_weight,但它也不起作用,这可能是因为它的父视图是垂直方向的.

longTextView.setLayoutParams(
        new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.MATCH_PARENT,
                0.8f)
);
Run Code Online (Sandbox Code Playgroud)

对我有用的是为长文本视图添加一些额外的视图.但是,为了尝试以百分比设置此视图的宽度,又添加了2个额外视图.有没有其他有效的方法来做到这一点?

<LinearLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:orientation="vertical"> …
Run Code Online (Sandbox Code Playgroud)

android android-linearlayout android-layout-weight

6
推荐指数
1
解决办法
6333
查看次数

如何使用layout_weight获得相同大小的宽度和高度[方形]?

我有以下代码:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1" >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Test 1"
        android:textSize="24sp" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1" >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Test 2"
        android:textSize="24sp" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

结果:

在此输入图像描述

如何将View的高度与其宽度(由此产生layout_weight)相等?

android android-layout-weight

5
推荐指数
2
解决办法
1万
查看次数

自定义view(扩展LinearLayout)“减肥”属性

所以..我正在定义一个自定义视图/布局,如下所示:

<?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"
android:weightSum="3">

<Button
    android:id="@+id/option_a"
    style="?android:selectableItemBackground"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="One"
    android:background="?android:selectableItemBackground"
    android:textAppearance="?android:attr/textAppearanceButton"
    android:textColor="@android:color/secondary_text_dark" />

<Button
    android:id="@+id/option_b"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Two"
    android:background="?android:selectableItemBackground"
    android:textColor="@android:color/holo_red_dark"
    android:textAppearance="?android:attr/textAppearanceButton" />

<Button
    android:id="@+id/option_c"
    style="?android:selectableItemBackground"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Three"
    android:background="?android:selectableItemBackground"
    android:textAppearance="?android:attr/textAppearanceButton"
    android:textColor="@android:color/secondary_text_dark" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

结果:

在此输入图像描述

但随后我在另一个布局中添加自定义视图,如下所示:

<com.xxx.xxx.xxx.ThreeStatePreference
    android:id="@+id/mode"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
Run Code Online (Sandbox Code Playgroud)

结果:

在此输入图像描述

看起来这完全忽略了“重量”属性。我究竟做错了什么?


PD:我找到了一个代码解决方案:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int wspec = MeasureSpec.makeMeasureSpec(getMeasuredWidth()/getChildCount(), MeasureSpec.EXACTLY);
    int hspec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY);

    for(int i=0; i<getChildCount(); i++){
        View v = getChildAt(i);
        v.measure(wspec, …
Run Code Online (Sandbox Code Playgroud)

android android-custom-view android-layout android-layout-weight

5
推荐指数
0
解决办法
617
查看次数

Android布局:在两个视图之间放置一个视图

我试图制作一个屏幕,最好只使用XML,看起来像这样: 在此输入图像描述

我遵循这种方法,但使用FrameLayout我只能选择使用"橙色"视图layout_gravity,当我尝试在图像中解释时,我不知道布局的高度,因为两者都用layout_weight来衡量.

我的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="onit.radiolisten.MainActivity">

    <FrameLayout
        android:id="@+id/layout_container_activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <!-- TOP LAYOUT, THE GREEN ONE ON THE IMAGE -->
            <LinearLayout
                android:id="@+id/layout_top_activity_main"
                android:layout_width="match_parent"
                android:layout_height="0dip"
                android:layout_weight="2"
                android:background="@color/colorPrimary"
                android:orientation="vertical"></LinearLayout>

            <!-- BOTTOM LAYOUT, THE BLUE ONE ON THE IMAGE -->
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#e6e6e6">

            </RelativeLayout>
        </LinearLayout>

        <!-- THIS IS THE IMAGE I WANT TO POSITION -->
        <ImageView
            android:id="@+id/btn_play_radio"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:layout_gravity="center"
            android:src="@drawable/play_icon" />
    </FrameLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

这可以仅用XML完成吗?

xml android overlap android-layout android-layout-weight

5
推荐指数
1
解决办法
2693
查看次数

如果我在键盘弹出时设置linearlayout权重,则会调整其大陆的大小

我遵循了这个示例并设法将fab按钮放置在正确的位置

在此处输入图片说明

但是当我按下编辑文本键盘时,会弹出并弄乱布局的权重

在此处输入图片说明

我怎样才能使ImageView保持静态,而当键盘打开时它将保持相同的大小?

XML:

<android.support.design.widget.CoordinatorLayout    
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:focusableInTouchMode="true">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
android:id="@+id/ViewA"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.6"
android:background="@color/colorPrimary"
android:orientation="horizontal">

<ImageView
    android:id="@+id/imgview"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:background="@color/light_blue"
    android:scaleType="centerCrop"
    android:src="@android:drawable/ic_menu_camera"
    android:layout_gravity="top|center_horizontal"
    />

</LinearLayout>

<LinearLayout
    android:id="@+id/ViewB"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="0.3"
    android:orientation="horizontal">
</LinearLayout>
</LinearLayout>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/add_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/fab_margin"
    app:layout_anchor="@id/ViewA"
    android:clickable="true"
    app:layout_anchorGravity="bottom|right"
    app:elevation="5dp"
    app:pressedTranslationZ="0dp"
    android:src="@android:drawable/ic_menu_camera"/>


</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

android imageview android-layout android-linearlayout android-layout-weight

5
推荐指数
1
解决办法
860
查看次数

android:weight 在 JavaFX 中的修正

我正在开发一个用于 JavaFX 培训的小软件。

在 Android 中,在 a 中LinearLayout,我可以通过设置变量来扩展视图以填充整个可用空间android:weight="someValue",不幸的是我无法实现这是 JavaFX。

我尝试使用最大/最小/首选高度,但没有用。

任何帮助将非常感激。

java android javafx android-layout-weight

5
推荐指数
1
解决办法
1010
查看次数

水平 android:animateLayoutChanges="true" 动画不流畅

我有一个动画布局,如下所示

在此输入图像描述

txt_billion使用android:animateLayoutChanges="true"(下面的布局代码)动态显示。

注意Hundred是跳跃的(实际上所有的都在跳跃,只是Hundred更明显)。如何防止文字跳动?

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:animateLayoutChanges="true"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:padding="8dp"
        android:background="#9f9"
        android:text="Hundreds" />
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:padding="8dp"
        android:background="#f9f"
        android:text="Thousands" />
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:padding="8dp"
        android:background="#0ff"
        android:text="Millions" />
    <TextView
        android:id="@+id/txt_billion"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:padding="8dp"
        android:visibility="gone"
        android:background="#ff0"
        android:text="Billions" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

您可以从https://github.com/elye/issue_horizo ​​ntal_layout_animate 获取代码进行测试

android android-animation android-layout android-layout-weight android-layoutparams

5
推荐指数
1
解决办法
2074
查看次数