我的要求是在LinearLayout / RelativeLayout内部具有动态TextView,以模仿“打包”链样式在约束布局中的工作方式,而不会传播。
通过我的尝试,它仅在通过XML添加TextViews时有效,而在以编程方式添加TextViews时则无效。
xml代码
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/app_bar_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="@id/answer_container"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="@+id/answer_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:background="@android:color/darker_gray"
android:gravity="center"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
>
<TextView
android:id="@+id/tv1"
style="@style/App_SquareTvStyle" />
<TextView
android:id="@+id/tv2"
style="@style/App_SquareTvStyle" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
normal_tv.xml(布局)
<?xml version="1.0" encoding="utf-8"?>
<TextView android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
style="@style/App_SquareTvStyle"
xmlns:android="http://schemas.android.com/apk/res/android" />
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
<style name="App_SquareTvStyle" parent="Widget.AppCompat.TextView.SpinnerItem">
<item name="android:layout_height">64dp</item> <!--for square-->
<item name="android:layout_width">64dp</item> <!--for square-->
<item name="android:text">1</item>
<item name="android:maxLines">1</item>
<item name="android:gravity">center</item>
<item name="android:textSize">16dp</item>
<item name="android:layout_margin">4dp</item>
<item name="android:autoSizeMaxTextSize">16dp</item>
<item name="android:autoSizeMinTextSize">6dp</item>
<item name="android:autoSizeStepGranularity">0.5sp</item>
<item name="android:autoSizeTextType">uniform</item>
<item name="android:background">@drawable/cell_borders</item>
<item name="android:textColor">@android:color/white</item>
</style>
</resources>
MainActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
answer_container = (LinearLayout) findViewById(R.id.answer_container);
int pos;
// following code overlaps subsequent textviews and
// finally only one textview is visble in center,
// but getChildCount returns 4
for(int i=12;i<=15;i++){
Log.d(TAG, "loadAnswerFromCrossWord: "+ Integer.toString(i));
//TextView tmp = new TextView(this);
TextView tmp = (TextView) getLayoutInflater().inflate(R.layout.normal_tv, answer_container,false);
int newId = View.generateViewId();
tmp.setId(newId);
tmp.setText(Integer.toString(i));
pos=answer_container.getChildCount();
answer_container.addView(tmp,pos);
}
}