Android 如何在LinearLayout中的任何TextView中使用属性重力

Android 如何在LinearLayout中的任何TextView中使用属性重力,android,android-layout,android-linearlayout,gravity,linearlayoutmanager,Android,Android Layout,Android Linearlayout,Gravity,Linearlayoutmanager,你能解释一下为什么我的文本视图不能在左边的重力上工作吗 A:线性布局|方向=水平|重力A=顶部或中心或底部 B:线性布局|方向=垂直|重力B=左或中或右 C:文本视图 我希望我们: A.设置重力(重力A) B.重力(重力B) 结果: 顶部|左侧:不工作(=顶部中心) 中|左:不工作(=中) 底部|左侧:不工作(=底部中心) 顶部|中心:工作 中心|中心:工作 底部|中心:工作 右上方:工作 LinearLayout parent = new LinearLayout(mContext

你能解释一下为什么我的文本视图不能在左边的重力上工作吗

A:线性布局|方向=水平|重力A=顶部或中心或底部

B:线性布局|方向=垂直|重力B=左或中或右

C:文本视图

我希望我们:

A.设置重力(重力A)

B.重力(重力B)

结果:

  • 顶部|左侧:不工作(=顶部中心)

  • 中|左:不工作(=中)

  • 底部|左侧:不工作(=底部中心)

  • 顶部|中心:工作

  • 中心|中心:工作

  • 底部|中心:工作

  • 右上方:工作

  •     LinearLayout parent = new LinearLayout(mContext);
    
    LinearLayout framelayoutTitle = new LinearLayout(mContext);
    framelayoutTitle.setOrientation(LinearLayout.HORIZONTAL);
    framelayoutTitle.setGravity(vGravityTitle);
    
    LinearLayout layoutTitle = new LinearLayout(mContext);
    layoutTitle.setOrientation(LinearLayout.VERTICAL);
    layoutTitle.setGravity(hGravityTitle);
    
    holder.textTitle = new TextView(mContext);
    layoutTitle.setLayoutParams( new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    
    
    framelayoutTitle.setLayoutParams(new LinearLayout.LayoutParams((widthTitle * (width - dp2px(widthImageLV))) / 100, dp2px(heightTitle)));    
    
    
            holder.textTitle.setText(titleList.get(position));
            holder.textTitle.setGravity(alignmentTextTitle);
            holder.textTitle.setLayoutParams( new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            holder.textTitle.setTextSize(sizeTitle);
            holder.textTitle.setBackgroundColor(Color.WHITE);
    
    layoutTitle.addView(holder.textTitle);
    framelayoutTitle.addView(layoutTitle);
    
    parent.setOrientation(LinearLayout.HORIZONTAL);
    parent.setGravity(Gravity.TOP);
    parent.addView(framelayoutTitle);
    
  • 中|右:工作

  •     LinearLayout parent = new LinearLayout(mContext);
    
    LinearLayout framelayoutTitle = new LinearLayout(mContext);
    framelayoutTitle.setOrientation(LinearLayout.HORIZONTAL);
    framelayoutTitle.setGravity(vGravityTitle);
    
    LinearLayout layoutTitle = new LinearLayout(mContext);
    layoutTitle.setOrientation(LinearLayout.VERTICAL);
    layoutTitle.setGravity(hGravityTitle);
    
    holder.textTitle = new TextView(mContext);
    layoutTitle.setLayoutParams( new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    
    
    framelayoutTitle.setLayoutParams(new LinearLayout.LayoutParams((widthTitle * (width - dp2px(widthImageLV))) / 100, dp2px(heightTitle)));    
    
    
            holder.textTitle.setText(titleList.get(position));
            holder.textTitle.setGravity(alignmentTextTitle);
            holder.textTitle.setLayoutParams( new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            holder.textTitle.setTextSize(sizeTitle);
            holder.textTitle.setBackgroundColor(Color.WHITE);
    
    layoutTitle.addView(holder.textTitle);
    framelayoutTitle.addView(layoutTitle);
    
    parent.setOrientation(LinearLayout.HORIZONTAL);
    parent.setGravity(Gravity.TOP);
    parent.addView(framelayoutTitle);
    
  • 右下:工作

        LinearLayout parent = new LinearLayout(mContext);
    
    LinearLayout framelayoutTitle = new LinearLayout(mContext);
    framelayoutTitle.setOrientation(LinearLayout.HORIZONTAL);
    framelayoutTitle.setGravity(vGravityTitle);
    
    LinearLayout layoutTitle = new LinearLayout(mContext);
    layoutTitle.setOrientation(LinearLayout.VERTICAL);
    layoutTitle.setGravity(hGravityTitle);
    
    holder.textTitle = new TextView(mContext);
    layoutTitle.setLayoutParams( new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    
    
    framelayoutTitle.setLayoutParams(new LinearLayout.LayoutParams((widthTitle * (width - dp2px(widthImageLV))) / 100, dp2px(heightTitle)));    
    
    
            holder.textTitle.setText(titleList.get(position));
            holder.textTitle.setGravity(alignmentTextTitle);
            holder.textTitle.setLayoutParams( new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            holder.textTitle.setTextSize(sizeTitle);
            holder.textTitle.setBackgroundColor(Color.WHITE);
    
    layoutTitle.addView(holder.textTitle);
    framelayoutTitle.addView(layoutTitle);
    
    parent.setOrientation(LinearLayout.HORIZONTAL);
    parent.setGravity(Gravity.TOP);
    parent.addView(framelayoutTitle);
    

试试这个,并根据需要提供填充和边距

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView"
    android:text="A"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView2"
    android:text="b"
    android:layout_below="@+id/textView"
    />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView3"
    android:text="b"
    android:layout_below="@+id/textView2"
    android:layout_centerHorizontal="true"
    />

</RelativeLayout>


取代所有这些简单的XML代码将是原来的10倍。不,我需要在布局A中的all位置移动文本视图。我不使用XML我使用编程代码我不知道您的XML代码。如果对上述代码没有任何概念,请发布您的代码,我进行更改并给出。不,我需要移动文本视图在布局A中的所有位置上,我不使用XML,我使用编程方式编码您想要执行的代码,用户可以在运行时更改文本框的位置。它像动画。不是动画,而是可以改变文本视图的位置:例如:如果我像这样设置重力:A.setGravity(Gravity.TOP);B.设置重力(重力左);通常,我会在左边显示文本视图的结果,并提供xml代码,然后更好地了解您所做的工作。