Android 如何按代码更改自定义视图布局的方向?
我创建了具有以下布局的自定义视图:Android 如何按代码更改自定义视图布局的方向?,android,Android,我创建了具有以下布局的自定义视图: <?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_h
<?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>
但当我尝试使用垂直方向时,如下所示:
<ivanapp.SimpleValueViewer
android:id="@+id/price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:label="Цена"/>
UI中的效果如下:
i、 e“文本”部分不存在。
我做了很多调试,但是我被卡住了
有什么帮助吗?事实上,这比我想的要容易: 1) 我刚刚创建了两个布局文件-一个用于水平布局,一个用于垂直布局 2) 我只是加载我需要的文件
仅此而已:)事实上,这比我想象的要容易: 1) 我刚刚创建了两个布局文件-一个用于水平布局,一个用于垂直布局 2) 我只是加载我需要的文件 就这些:)
<ivanapp.SimpleValueViewer
android:id="@+id/price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:label="Цена"/>
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="Цена"/>