簡體   English   中英

如何通過代碼更改自定義視圖布局的方向?

[英]How to change orientation of custom view layout by code?

我創建了一個具有以下布局的自定義視圖:

<?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="wrap_content"
    android:orientation="horizontal"
    android:gravity="center_vertical"
    android:paddingHorizontal="10dp"
    android:paddingVertical="5dp"
    android:weightSum="3">

    <include
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        layout="@layout/layout_field_label"
        android:layout_weight="2" />

    <TextView
        android:id="@+id/valueTextView"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:inputType="numberDecimal"
        android:textColor="@color/colorValue"
        android:textSize="20dp"
        android:textAlignment="textEnd"
        android:text="@string/simpleValueViewer_text"
        android:layout_weight="1"/>
</LinearLayout>

包含的布局如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/ex_label"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp"
        android:layout_marginBottom="5dp"
        android:text="@string/simpleValueEditor_label" />

    <ImageView
        android:id="@+id/infoIcon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:src="@drawable/ic_info_icon_16dp"/>
</LinearLayout>

我以這種方式在片段中使用自定義視圖:

<ivanapp.SimpleValueViewer
    android:id="@+id/price"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:label="Цена"/>

UI 中的結果是這樣的: 在此處輸入圖片說明 即當我以默認的水平方向使用它時,這就是我想要的。

但我想擴展自定義視圖以配置它的方向:當它是垂直的時,“文本”部分應該在“標簽”部分下。 所以我把下面的方法放在類中並從構造函數調用這個方法:

protected void initEx(Context context, @Nullable AttributeSet attrs) {
        LayoutInflater.from(context).inflate(R.layout.view_simple_value_viewer, this, 
  true);
        mValueTextView = findViewById(R.id.valueTextView);
        TypedArray attributes = context.obtainStyledAttributes(attrs, 
  R.styleable.SimpleValues);
        mLabel.setText(attributes.getString(R.styleable.SimpleValues_android_label));
        mValueTextView.setText(attributes.getString(R.styleable.SimpleValues_value));
        LinearLayout labelEx = findViewById(R.id.ex_label);
        switch(getOrientation()) {
            case LinearLayout.VERTICAL: {
                LinearLayout.LayoutParams labelParams = new LinearLayout.LayoutParams(
                        LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 0
                );
                labelEx.setLayoutParams(labelParams);
                LinearLayout.LayoutParams valueParams = new LinearLayout.LayoutParams(
                        LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 0
                );
                mValueTextView.setLayoutParams(valueParams);
                mValueTextView.setTop(labelEx.getHeight());
                setWeightSum(0);
                break;
            }
            case LinearLayout.HORIZONTAL: {
                LinearLayout.LayoutParams labelParams = new LinearLayout.LayoutParams(
                        0, LayoutParams.WRAP_CONTENT, 2
                );
                labelEx.setLayoutParams(labelParams);
                LinearLayout.LayoutParams valueParams = new LinearLayout.LayoutParams(
                        0, LayoutParams.WRAP_CONTENT, 1
                );
                mValueTextView.setLayoutParams(valueParams);
                setWeightSum(labelParams.weight + valueParams.weight);
                break;
            }
        }
        attributes.recycle();
}

但是當我嘗試像這樣以垂直方向使用它時:

<ivanapp.SimpleValueViewer
    android:id="@+id/price"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:label="Цена"/>

UI中的效果是這樣的: 在此處輸入圖片說明 即“文本”部分不存在。 我已經做了很多調試,但我被卡住了。

有什么幫助嗎?

事實上,它比 Ithough 更容易:

1) 我剛剛創建了 2 個布局文件 - 一個用於水平,一個用於垂直。

2)我只是加載我需要的文件。

就這樣 :)

暫無
暫無

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

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